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