| // SAP送信はWF値を送信しないので、before更新でも問題ない | 
| trigger NFM001AgencyContract on Account (before insert, before update, after insert) { | 
|     if (StaticParameter.EscapeNFM001AgencyContractTrigger) { | 
| System.debug('Escape、EscapeNFM001AgencyContractTrigger:::::' + StaticParameter.EscapeNFM001AgencyContractTrigger); | 
|         return; | 
|     } | 
|      | 
|     // WFなど設定により、2回目呼び出される場合もある、2回目をここに入らないように NFM001Controller.isRunning を判断する | 
|     if (NFM001Controller.isRunning) { | 
|         return; | 
|     } | 
|   | 
|     // 更新対象 Id をセット | 
|     List<String> accIds = new List<String>(); | 
|     Map<String, String> purposeOfAdviceMap = new Map<String, String>();    // 1:Delete 2:Add(Insert) 3:Change(Update) | 
|   | 
| //    if (NFM001Controller.isInsert && Trigger.isUpdate) { | 
| //        NFM001Controller.isRunning = true; | 
| //        // insertだけど、WFにて更新項目があって、isUpdateになります。 | 
| //        for(Account a : Trigger.new) { | 
| //            if (String.isBlank(a.AgencyContract_Management_Code__c)) { | 
| //                accIds.add(a.Id); | 
| //                purposeOfAdviceMap.put(a.Id, '2'); | 
| //                NFM001Controller.debug_msg = 'NFM001_callout_insert_' + a.Name; | 
| //            } else { | 
| //                accIds.add(a.Id); | 
| //                purposeOfAdviceMap.put(a.Id, '3'); | 
| //                NFM001Controller.debug_msg = 'NFM001_callout_update_' + a.Name; | 
| //            } | 
| //        } | 
| //    } | 
|   | 
|     // 送信対象のレコードタイプ | 
|     String[] needSendTypes = new String[] {'契約', '販売店'}; | 
|     List<RecordType> rects = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :needSendTypes]; | 
|     Map<String, String> needSendRectMap = new Map<String, String>(); | 
|     for (RecordType rect : rects) { | 
|         needSendRectMap.put(rect.Id, rect.Name); | 
|     } | 
|      | 
|     if (Trigger.isInsert) { | 
|         if (Trigger.isBefore) { | 
|             System.debug(Logginglevel.DEBUG, 'NFM001AgencyContract isBefore isInsert'); | 
|         } else { | 
|             System.debug(Logginglevel.DEBUG, 'NFM001AgencyContract isAfter isInsert'); | 
| //            // TODO 重複チェック | 
| //            Boolean hasError = false; | 
| //            Map<String, Account> agentStateIds = new Map<String, Account>(); | 
| //            for(Account a : Trigger.new) { | 
| //                if (needSendRectMap.get(a.RecordTypeId) == '契約') { | 
| //                    agentStateIds.put(a.ParentId + ':' + a.State_Master__c, a); | 
| //                    System.debug(Logginglevel.DEBUG, 'agentStateIds.add <== ' + a.ParentId + ':' + a.State_Master__c); | 
| //                } | 
| //            } | 
| //            List<Account> doubleChildList = ControllerUtil.getDoubleChildForAgent(agentStateIds); | 
| //            for (Account doubleA : doubleChildList) { | 
| //                Account a = agentStateIds.get(doubleA.Agency_State_Master__c); | 
| //                if (String.isBlank(a.AgencyContract_Management_Code__c)) { | 
| //                    a.addError('当前数据 ' + a.Id + ' 和 ' + doubleA.Id + ' 的数据处于同一个省,不能使用新的客户编码。'); | 
| //                    NFM001Controller.debug_msg = 'NFM001_AgencyContract_Management_Code__c_double 1'; | 
| //                    hasError = true; | 
| //                } else if (a.AgencyContract_Management_Code__c != doubleA.AgencyContract_Management_Code__c) { | 
| //                    a.addError('当前数据 ' + a.Id + ' 和 ' + doubleA.Id + ' 的数据处于同一个省,但是客户编码不同。'); | 
| //                    NFM001Controller.debug_msg = 'NFM001_AgencyContract_Management_Code__c_double 2'; | 
| //                    hasError = true; | 
| //                } | 
| //            } | 
| //            if (!hasError) { | 
|                   for(Account a : Trigger.new) { | 
|                       if (needSendRectMap.get(a.RecordTypeId) == null || needSendRectMap.get(a.RecordTypeId) == '販売店') { | 
|                           continue; | 
|                       } | 
|                       NFM001Controller.isRunning = true; | 
|                       System.debug(Logginglevel.DEBUG, 'NFM001AgencyContract isInsert a.AgencyContract_Management_Code__c::::' + a.AgencyContract_Management_Code__c); | 
|                       if (String.isBlank(a.AgencyContract_Management_Code__c)) { | 
|                           accIds.add(a.Id); | 
|                           purposeOfAdviceMap.put(a.Id, '2'); | 
|                           NFM001Controller.debug_msg = 'NFM001_callout_insert_' + a.Name; | 
|                       } else { | 
|                           accIds.add(a.Id); | 
|                           purposeOfAdviceMap.put(a.Id, '3'); | 
|                           NFM001Controller.debug_msg = 'NFM001_callout_update_' + a.Name; | 
|                       } | 
|                   } | 
| //            } | 
|         } | 
|     } else if (Trigger.isUpdate && NFM001Controller.isRunning == false) { | 
|         List<String> agentIds = new List<String>(); | 
|         for(Account a : Trigger.new) { | 
|             if (needSendRectMap.get(a.RecordTypeId) == null) { | 
|                 continue; | 
|             } | 
|   | 
|             // NFM001の送信項目として、isChanged かを確認 | 
|             Boolean isChanged = false; | 
|             Boolean needChild = false; | 
|             // TODO 論理削除/復活の対応 | 
|              | 
|             // TODO 下記の項目変更を対応する | 
|             // Name, City_Master__c, Parent | 
|             if (Trigger.oldMap.get(a.Id).get('OCM_Category__c') != a.OCM_Category__c | 
|                     && isChanged == false) {   // 実はいらないけど、再送信がdataloaderにてできるために使う | 
|                 isChanged = true; | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('Name') != a.Name | 
|                     && isChanged == false) { | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '販売店') { | 
|                     isChanged = true; | 
|                     needChild = true; | 
|                     NFM001Controller.debug_msg = 'Name_' + a.Name; | 
|                 } | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('City_Master__c') != a.City_Master__c | 
|                     && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'City_Master__c'; | 
|             } | 
|             if (Trigger.oldMap.get(a.Id).get('ParentId') != a.ParentId | 
|                     && isChanged == false) { | 
|                 isChanged = true; | 
|                 NFM001Controller.debug_msg = 'Parent'; | 
|             } | 
|              | 
|             if (isChanged) { | 
|                 NFM001Controller.isRunning = true; | 
|                 if (needSendRectMap.get(a.RecordTypeId) == '契約') accIds.add(a.Id); | 
|                 purposeOfAdviceMap.put(a.Id, '3'); | 
|                 NFM001Controller.debug_msg += '_NFM001_callout_update'; | 
|                 if (needChild) { | 
|                     agentIds.add(a.Id); | 
|                 } | 
|             } | 
|              | 
|             // 契約の空更新 と 販売店毎に、作成日最新の契約を送信するため purposeOfAdviceMap にデータをセット | 
|             ControllerUtil.collectChildIdForAgent(accIds, purposeOfAdviceMap, agentIds); | 
|   | 
|         } | 
|   | 
|     } | 
|      | 
|     System.debug(Logginglevel.DEBUG, 'NFM001AgencyContract accIds.size():::::' + accIds.size()); | 
|   | 
|     if (accIds.size() > 0) { | 
|         //----------XinHongLu--------------------------AddStart-------------------20180914------------------------------ | 
|         // MessageGroupNumber の採番 | 
|         if (StaticParameter.EscapeNFM001AgencyContractTrigger2) { | 
|             System.debug('Escape、EscapeNFM001AgencyContractTrigger2:::::' + StaticParameter.EscapeNFM001AgencyContractTrigger2); | 
|             return; | 
|         } | 
|         //----------XinHongLu--------------------------EndStart-------------------20180914------------------------------ | 
|         BatchIF_Log__c iflog = new BatchIF_Log__c(); | 
|         iflog.Type__c = 'NFM001'; | 
|         iflog.Log__c  = 'callout start\n'; | 
|         insert iflog; | 
|         iflog = [Select Id, Name from BatchIF_Log__c where Id = :iflog.Id]; | 
|         System.debug(Logginglevel.DEBUG, 'NFM001_' + iflog.Name + ' start');                  // callout の中 end のlogを出します | 
|         NFM001Controller.callout(iflog.Id, accIds, purposeOfAdviceMap, needSendRectMap); | 
|          | 
|     } | 
| } |