Loading
Beispielcode zum Aktualisieren der nicht synchronisierten Zuordnung von Vertragstypen

Beispielcode zum Aktualisieren der nicht synchronisierten Zuordnung von Vertragstypen

Wenn Sie eine Datensatztypzuordnung mit einem Kontakttyp löschen, hinzufügen oder ändern, gelten die Änderungen nur für die neuen Verträge. Zum Aktualisieren der nicht synchronisierten Verträge kann der Systemadministrator einen benutzerdefinierten Code ausführen.

Der Systemadministrator kann diesen Beispielcode verwenden, um die Vertragszuordnung für bestehende Verträge zu ändern. Öffnen Sie die Developer Console, wechseln Sie zu Debug Open Execute Anonymous Window (Offenes Fenster zur anonymen Ausführung öffnen), fügen Sie den Beispielcode ein und klicken Sie auf Execute (Ausführen). Weitere Informationen zur Verwendung von Apex Class finden Sie im Apex Developer Guide.

Anwendungsfall: Vertragstyp Gesundheit entspricht den Datensatztypen "Stationäre Pflege" und "Ambulante Pflege". Es gibt 1000 Vertragsdatensätze, die diese Zuordnung aufweisen. Sie ändern die Zuordnung für den Datensatztyp "Patientenversorgung" zu "Vertragstyp Umsatz". Von den vorhandenen 1000 Datensätzen gibt es 500 Datensatztypen für die stationäre Versorgung, die auf "Vertragstyp – Gesundheit" verweisen. In den Vertragszeilen wird weiterhin der Vertragstyp "Gesundheit" und der Datensatztyp "Patientenversorgung" angezeigt, da die Salesforce Contracts-Anwendung die Vertragsdatensätze mit dem Datensatztyp "Patientenversorgung" und dem Vertragstyp "Umsatz" nicht automatisch aktualisiert. Nur der Systemadministrator kann die Änderungen mithilfe eines Beispielcodes anwenden. Wenn jedoch ein neuer Vertrag mit dem Datensatztyp "Stationäre Pflege" erstellt wird, lautet der Vertragstyp "Umsatz".

Hinweis
Hinweis Das Feld "Vertragstyp" im Vertragsobjekt ist standardmäßig für alle Nicht-Administratorprofile schreibgeschützt. Auf Profilebene können Benutzer basierend auf der Feldebenensicherheit den nicht synchronisierten Vertragstyp für das Vertragsobjekt manuell oder mithilfe eines Apex Codes aktualisieren.

Der Beispielcode ruft eine Liste der Vertragstypen ab. Sie können beliebig viele Vertragstypen übergeben und die Zuordnung anpassen. Sie können die Abfrage bearbeiten, um Änderungen vorzunehmen und Verträge mit bestimmten Status wie "Entwurf", "Vertrag abgelaufen", "Abgebrochen" usw. abzurufen.

public class SyncContractTypes {
   
	// use-case1 : sync contracts when existing RecordType that's associated with contracts is newly mapped to a existing ContractType
	// use-case2 : sync contracts when existing RecordType that's associated with contracts is removed from ContractType object - ContractType, SubType mapping
	// use-case3 : sync contracts when existing RecordType that's associated with contracts is removed from ContractType object - ContractType, SubType mapping  and mapped to existing ContractType, SubType
   
	public static void syncOutOfSyncContractTypes(List<String> contractTypes) {
       
    	Map<String,String> contractTypeSubtypeMap = new Map<String,String>();
    	Map<String,String> subtypeContractTypeMap = new Map<String,String>();
       
    	if(contractTypes != null && !contractTypes.isEmpty()) {
       	// Here we are querying all the contractTypes to support a use-case2
       	// where a SubType is removed from a contractType and mapped to another existing contractType
       	// If customers don't want to update when existing subType is mapped to another existing contractType they can
       	// restrict this by adding a where condition, WHERE MasterLabel IN :contractTypes
        	List<ContractType> contractTypeList = [SELECT MasterLabel,SubTypes FROM ContractType];
           
        	if(contractTypeList!= null && !contractTypeList.isEmpty()) {
            	for( ContractType c : contractTypeList) {
                	contractTypeSubtypeMap.put(c.MasterLabel, c.SubTypes);
                	if(c.subTypes != null) {
                    	for(String s : c.subTypes.split(',')) {
                        	subtypeContractTypeMap.put(s, c.MasterLabel);
                    	}
                	}
            	}
        	}
           
        	for(String ct : contractTypes) {
           
            	String contractType = (contractTypeSubtypeMap.containsKey(ct)) ? ct : '';
               
            	List<String> subTypesList = (contractTypeSubtypeMap.get(ct) != null) ? contractTypeSubtypeMap.get(ct).split(',') : new List<String>();
               
            	// we are applying basic filters here, please modify them accordingly to your use case
            	List<Contract> contracts = [
                	SELECT Id, ContractType, Name, RecordType.Name
                	FROM Contract
                	WHERE (ContractType =: contractType OR RecordType.Name IN :subTypesList)
                        	AND IsAssociatedWithCLM = true
                        	AND (NOT (ContractType =: contractType AND RecordType.Name IN : subTypesList))];
               
            	List<Contract> contractsToUpdate = new List<Contract>();
            	// check if any of the contracts are out of sync and update them
            	for(Contract c : contracts) {
                	String RecordTypeName = c.RecordType.Name;
                	if(subTypesList.contains(RecordTypeName) && c.ContractType != contractType) {
                    	c.ContractType = contractType;
                    	contractsToUpdate.add(c);
                	} else if(!subTypesList.contains(RecordTypeName) ){
                    	// check to see if any other ContractTypes has this RecordType as SubType
                    	c.ContractType = (subtypeContractTypeMap.get(RecordTypeName) != null) ? subtypeContractTypeMap.get(RecordTypeName) : '';
                    	contractsToUpdate.add(c);
                	}
                   
            	}           
            	if(contractsToUpdate != null && !contractsToUpdate.isEmpty()) {
                   
                	Database.SaveResult[] saveResult = Database.Update(contractsToUpdate, false);
                	for (Database.SaveResult sr : saveResult) {
                    	if (sr.isSuccess()) {
                        	// Operation was successful, so get the ID of the record that was processed
                        	System.debug('Successfully updated contract. Contract ID: ' + sr.getId());
                    	}
               	     else {
                        	// Operation failed, so get all errors               
                        	for(Database.Error err : sr.getErrors()) {
                            	System.debug('The following error has occurred.');    	            
                            	System.debug(err.getStatusCode() + ': ' + err.getMessage());
                            	System.debug('Contract fields that affected this error: ' + err.getFields());
                        	}
                   	}
                	}
            	}
        	}
  	}
	}
}
 
Laden
Salesforce Help | Article