위치:
동기화되지 않은 계약 유형 매핑을 업데이트하기 위한 샘플 코드
연락처 유형으로 레코드 유형 매핑을 삭제, 추가 또는 수정하는 경우 변경 사항은 새 계약에만 적용됩니다. 동기화되지 않은 계약을 업데이트하기 위해 시스템 관리자가 사용자 정의 코드를 실행할 수 있습니다.
시스템 관리자는 이 샘플 코드를 사용하여 기존 계약에 대한 계약 매핑을 수정할 수 있습니다. Developer Console을 열고 Debug Open Execute Anonymous Window로 이동하여 샘플 코드를 붙여넣고 Execute를 클릭합니다. Apex 클래스 사용에 대한 자세한 내용은 Apex 개발자 가이드를 참조하십시오.
사용 사례: 계약 유형 건강은 기록 유형의 입원 환자 치료 및 입원 환자 치료에 해당합니다. 이 매핑이 포함된 계약 레코드가 1,000개 있습니다. 레코드 유형 인치케어의 매핑을 계약 유형 수입으로 변경하면 기존 1000개의 레코드 중 500개의 인치케어 레코드 유형이 계약 유형 건강을 가리킵니다. Salesforce Contracts 응용 프로그램이 계약 유형이 Revenue인 레코드 유형이 있는 계약 레코드를 자동으로 업데이트하지 않으므로 계약 행은 계속해서 계약 유형을 Health로 표시하고 레코드 유형을 Inpatient Care로 표시합니다. 시스템 관리자만 샘플 코드를 사용하여 변경 사항을 적용할 수 있습니다. 그러나 레코드 유형이 입원 케어인 새 계약이 생성되면 계약 유형은 매출입니다.
샘플 코드는 계약 유형의 목록을 가져옵니다. 여러 개의 계약 유형을 전달하고 매핑을 조정할 수 있습니다. 쿼리를 편집하여 초안, 계약 만료, 취소 등과 같은 특정 상태의 계약을 변경하고 가져올 수 있습니다.
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());
}
}
}
}
}
}
}
}
