Loading
更新不同步的契約類型對應範例程式碼

更新不同步的契約類型對應範例程式碼

如果您刪除、新增或修改具有連絡類型的記錄類型對應,則變更只會套用至新契約。若要更新不同步的契約,系統管理員可以執行自訂程式碼。

系統管理員可以使用此範例程式碼修改現有契約的契約對應。開啟「開發人員主控台」,移至「除錯」 「開啟執行匿名視窗」,貼上範例程式碼,然後按一下「執行」。如需使用 Apex 類別的詳細資訊,請參閱《Apex 開發人員指南》。

使用個案:契約類型 健康狀況對應於記錄類型 住院照護和門診照護。有 1000 個契約記錄具有此對應。您將記錄類型的「 住院照護 」對應變更為契約類型「 收入 」,且現有 1000 筆記錄中有 500 個指向「契約類型健康」的「住院照護」記錄類型。由於 Salesforce Contracts 應用程式不會自動更新具有記錄類型為「住院照護」的契約記錄,因此契約列會繼續將契約類型顯示為「健康」和記錄類型顯示為「住院照護」。只有系統管理員可以使用範例程式碼來套用變更。然而,當使用記錄類型為「門診照護」建立新契約時,則契約類型將為「收入」。

備註
備註 所有非管理員設定檔的契約物件上的「契約類型」欄位預設為唯讀。在設定檔層級,根據欄位級安全性,使用者可以手動或使用 Apex 程式碼更新契約物件上的非同步化契約類型。

範例程式碼會提取「契約類型」清單。您可以傳遞任何數量的「契約類型」並調整對應。您可以編輯查詢來進行變更,並提取特定狀態 (例如「草稿」、「契約已到期」、「已取消」等) 的契約。

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());
                        	}
                   	}
                	}
            	}
        	}
  	}
	}
}
 
正在載入
Salesforce Help | Article