Loading
メールアドレスがデフォルトのログイン方式となります 続きを読む
非同期契約種別の対応付けを更新するサンプルコード

非同期契約種別の対応付けを更新するサンプルコード

取引先責任者種別とのレコードタイプの対応付けを削除、追加、または変更した場合、変更は新しい契約にのみ適用されます。非同期契約を更新するには、システム管理者がカスタムコードを実行できます。

システム管理者は、このサンプルコードを使用して既存の契約の対応付けを変更できます。開発者コンソールを開き、[デバッグ] [Open Execute Anonymous Window (実行匿名ウィンドウを開く)] に移動して、サンプルコードを貼り付け、[Execute (実行)] をクリックします。Apex クラスの使用の詳細については、『Apex 開発者ガイド』を参照してください。

使用事例: 契約タイプ Health は、レコードタイプ Inpatient Care と Outpatient Care に対応します。この対応付けがある契約レコードは 1000 件あります。レコードタイプ [Inpatient Care (入院患者ケア)] の契約タイプ [Revenue (収益)] への対応付けを変更すると、既存の 1000 件のレコードのうち、500 件の [Inpatient Care (入院患者ケア)] レコードタイプが契約タイプの [Health (正常性)] を参照します。Salesforce Contractsアプリケーションでは、契約種別が[Inpatient Care]、契約種別が[Revenue]の契約レコードは自動的に更新されないため、契約行には契約種別が[Health]、レコード タイプが[Inpatient Care]と表示されます。システム管理者のみがサンプルコードを使用して変更を適用できます。ただし、レコードタイプが [入院患者ケア] の新規契約が作成された場合、契約タイプは [収益] になります。

メモ
メモ デフォルトでは、契約オブジェクトの [契約種別] 項目は、すべての非管理者プロファイルで参照のみです。プロファイル レベルでは、項目レベル セキュリティに基づいて、ユーザーは契約オブジェクトの同期していない契約種別を手動または 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