U bent hier:
Voorbeeldcode voor het bijwerken van niet-synchrone toewijzing van contracttypen
Als u een recordtypetoewijzing met een contactpersoonstype verwijdert, toevoegt of wijzigt, worden de wijzigingen alleen toegepast op de nieuwe contracten. Voor het bijwerken van niet-synchrone contracten kan de systeembeheerder een aangepaste code uitvoeren.
De systeembeheerder kan deze voorbeeldcode gebruiken om de contracttoewijzing voor bestaande contracten te wijzigen. Open de Developer Console, ga naar Anoniem venster Foutopsporing openen, plak de voorbeeldcode en klik op Uitvoeren. Zie de Apex Developer Guide voor meer informatie over het gebruik van Apex Class.
Gebruikscase: Contracttype Gezondheid komt overeen met de recordtypen Klinische zorg en Klinische zorg. Er zijn 1000 contractrecords die deze toewijzing hebben. U wijzigt de toewijzing voor recordtype Klinische zorg in contracttype Omzet en van de bestaande 1000 records zijn er 500 recordtypen Klinische zorg die verwijzen naar Contracttype Gezondheid. De contractrijen blijven het contracttype Gezondheid tonen en het recordtype Klinische zorg, omdat de Salesforce Contracts toepassing niet automatisch de contractrecords bijwerkt die recordtype Klinische zorg hebben met contracttype Omzet. Alleen de systeembeheerder kan de wijzigingen toepassen met behulp van een voorbeeldcode. Wanneer er echter een nieuw contract wordt gemaakt met het recordtype Klinische zorg, is het contracttype Omzet.
De voorbeeldcode haalt een lijst van Contracttypen op. U kunt een willekeurig aantal contracttypen doorgeven en de toewijzing aanpassen. U kunt de query bewerken om wijzigingen aan te brengen en contracten met specifieke statussen op te halen, zoals Concept, Contract verlopen, Geannuleerd, enz.
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());
}
}
}
}
}
}
}
}
