| trigger SyncOpportunity on Opportunity (after delete, after insert, after update) { | 
|     if (StaticParameter.EscapeSyncOpportunityTrigger) { | 
| System.debug('Escape、EscapeSyncOpportunityTrigger:::::' + StaticParameter.EscapeSyncOpportunityTrigger); | 
|         return; | 
|     } | 
|      | 
|     List<String> oppIds = new List<String>(); | 
|     List<Opportunity> opps = new List<Opportunity>(); | 
| System.debug('SyncOpportunity start'); | 
|      | 
|     if (Trigger.isDelete) { | 
|         for (Opportunity opp : Trigger.old) { | 
|             oppIds.add(opp.Id); | 
|             opps.add(opp); | 
|         } | 
|     } else { | 
| System.debug('SyncOpportunity loop Trigger.new'); | 
|         for (Opportunity opp : Trigger.new) { | 
|             // 古いデータの更新を禁止 | 
|             if (Trigger.isUpdate) { | 
|                 Boolean noChange = false; | 
|                 Opportunity oldOpp = (Opportunity) Trigger.oldMap.get(opp.Id); | 
|                 for (String colApiName : ControllerUtil.oppColumnList) { | 
|                     colApiName = colApiName.trim(); | 
|                     if (opp.oldData_flg__c &&  | 
|                             Trigger.oldMap.get(opp.Id).get(colApiName) != Trigger.newMap.get(opp.Id).get(colApiName)) { | 
|                         noChange = true; | 
|                         break; | 
|                     } | 
|                 } | 
|                 if (noChange) { | 
|                     opp.addError('不能修改旧数据'); | 
|                     continue; | 
|                 } | 
|                 // 更新だけど、Opportunity2__c の同期対象になるか? | 
|                 if (oldOpp.OwnerId == opp.OwnerId | 
|                         && oldOpp.State_Text__c == opp.State_Text__c | 
|                         && oldOpp.StageName == opp.StageName | 
|                         && oldOpp.Wholesale_Price__c == opp.Wholesale_Price__c | 
|                         && oldOpp.Estimation_List_Price__c == opp.Estimation_List_Price__c | 
|                 ) { | 
|                     System.debug('Opportunity2__c の同期対象ではない!'); | 
|                     continue; | 
|                 } | 
|             } | 
| System.debug('SyncOpportunity sync target:id=' + opp.Id); | 
|             oppIds.add(opp.Id); | 
|             opps.add(opp); | 
|         } | 
|     } | 
|      | 
|     Map<String, Opportunity2__c> opp2Map = new Map<String, Opportunity2__c>(); | 
|     if (oppIds.size() == 0) return; | 
|     List<Opportunity2__c> opp2s = ControllerUtil.opp2SelectForSync(oppIds); | 
|     for (Opportunity2__c opp2 : opp2s) { | 
|         opp2Map.put(opp2.Opportunity__c, opp2); | 
|     } | 
|      | 
|     List<Opportunity2__c> insOpp2 = new List<Opportunity2__c>(); | 
|     List<Opportunity2__c> updelOpp2 = new List<Opportunity2__c>(); | 
|     for (Opportunity opp : opps) { | 
|         // 見付かったら更新/削除 | 
|         if (opp2Map.containsKey(opp.Id)) { | 
|             Opportunity2__c opp2 = opp2Map.get(opp.Id); | 
|             opp2.OwnerId = (opp.Owner_IsActive__c == true) ? opp.OwnerId : UserInfo.getUserId(); | 
|             opp2.Owner_System__c = (opp.Owner_IsActive__c == true) ? opp.OwnerId : UserInfo.getUserId(); | 
|             opp2.State_Text__c = opp.State_Text__c; | 
|             opp2.StageName_Text__c = opp.StageName; | 
|             opp2.Wholesale_Price_T__c = opp.Wholesale_Price__c; | 
|             opp2.Amount_Without_Tax_T__c = opp.Amount_Without_Tax__c; | 
|             opp2.Estimation_List_Price_T__c = opp.Estimation_List_Price__c; | 
|             opp2.Estimation_List_Price_Without_Tax_T__c = opp.Estimation_List_Price_Without_Tax__c; | 
|             updelOpp2.add(opp2); | 
|         // 新規 | 
|         } else { | 
|             Opportunity2__c opp2 = new Opportunity2__c( | 
|                 Opportunity__c = opp.Id, | 
|                 OwnerId = (opp.Owner_IsActive__c == true) ? opp.OwnerId : UserInfo.getUserId(), | 
|                 Owner_System__c = (opp.Owner_IsActive__c == true) ? opp.OwnerId : UserInfo.getUserId(), | 
|                 State_Text__c = opp.State_Text__c, | 
|                 StageName_Text__c = opp.StageName, | 
|                 CurrencyIsoCode = opp.CurrencyIsoCode, | 
|                 Wholesale_Price_T__c = opp.Wholesale_Price__c, | 
|                 Amount_Without_Tax_T__c = opp.Amount_Without_Tax__c, | 
|                 Estimation_List_Price_T__c = opp.Estimation_List_Price__c, | 
|                 Estimation_List_Price_Without_Tax_T__c = opp.Estimation_List_Price_Without_Tax__c | 
|             ); | 
|             insOpp2.add(opp2); | 
|         } | 
|     } | 
|      | 
|     if (Trigger.isDelete) ControllerUtil.delOpp2List(updelOpp2); | 
|     if (Trigger.isUpdate) ControllerUtil.updOpp2List(updelOpp2); | 
|     if (!Trigger.isDelete) ControllerUtil.insOpp2List(insOpp2); | 
| } |