| trigger LeadTrigger on Lead (before insert, before update) { | 
|   | 
|     if ( StaticParameter.EscapeLeadTrigger == true) { | 
|         System.debug( '-----:リードのトリガー動きません'); | 
|         return; | 
|     } | 
|     Lead old = null; | 
|     if(Trigger.isBefore){ | 
|         if(StartTradingController.EscapeLeadUpdate == false){ | 
|             List<String> accIdList = new List<String>(); | 
|             for(Lead l : Trigger.New){ | 
|                 accIdList.add(l.Hospital_Name__c); | 
|             } | 
|             Map<Id, Account> accountMap = new Map<Id, Account>(); | 
|             Map<String, OCM_Management_Province__c> provinceMap = new Map<String, OCM_Management_Province__c>(); | 
|             if (accIdList.size() > 0) { | 
|                 List<Account> accList = [select id, OCM_Management_Province_HP__c, Parent.Department_Class_Label__c from Account where id = :accIdList]; | 
|                 for (Account acc : accList) { | 
|                     accountMap.put(acc.id, acc); | 
|                 } | 
|                 List<OCM_Management_Province__c> provinceList = [select id, Name, GI_assistant__c, SP_assistant__c, Energy_assistant__c from OCM_Management_Province__c]; | 
|                 for (OCM_Management_Province__c province : provinceList) { | 
|                     provinceMap.put(province.Name, province); | 
|                 } | 
|             } | 
|             for(Lead l : Trigger.New){ | 
|                 if (Trigger.isUpdate) { | 
|                     old = Trigger.oldMap.get(l.Id); | 
|                 } | 
|                 if (Trigger.isInsert && l.Status == '確認済み') { | 
|                     l.Status.addError('意向转询价前,状态不能为开始询价'); | 
|                 } | 
|                 if (Trigger.isUpdate && l.Status != old.Status && l.Status == '確認済み') { | 
|                     l.Status.addError('意向转询价前,状态不能为开始询价'); | 
|                 } | 
|                 if (Trigger.isInsert){ | 
|                     if(l.Owner_Dep__c == '7.能量'){ | 
|                         String p = accountMap.get(l.Hospital_Name__c) == null ? '' : accountMap.get(l.Hospital_Name__c).OCM_Management_Province_HP__c; | 
|                         l.Energy_Assistant__c = provinceMap.get(p) == null ? null : provinceMap.get(p).Energy_assistant__c; | 
|                     }else{ | 
|                         String p = accountMap.get(l.Hospital_Name__c) == null ? '' : accountMap.get(l.Hospital_Name__c).OCM_Management_Province_HP__c; | 
|                         String c = accountMap.get(l.Hospital_Name__c) == null ? '' : accountMap.get(l.Hospital_Name__c).Parent.Department_Class_Label__c; | 
|                         if (c == '消化科' || c == '呼吸科' || c == 'ET') { | 
|                             l.OCM_Oppor_Assistant__c = provinceMap.get(p) == null ? null : provinceMap.get(p).GI_assistant__c; | 
|                         } else { | 
|                             l.OCM_Oppor_Assistant__c = provinceMap.get(p) == null ? null : provinceMap.get(p).SP_assistant__c; | 
|                         } | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|   | 
|     } | 
|     // ------------------------------------------------------------- | 
|     // リードのメールアドレスを元に、取引先責任者を関連付けます。 | 
|     // すでに関連づいている場合はなにもしません。 | 
|     // ------------------------------------------------------------- | 
|     // メールアドレスが一致する contactを探します。 | 
|     // 複数見つかる場合は、最終更新日が一番新しい contactにします | 
|     // メールアドレスが null のリードは無視します。 | 
|      | 
|     // メアド一覧を得る | 
|     // メアド一覧を元に contactを得る | 
|     // 更新対象のリードに、contactへの参照をセットする | 
|      | 
|     // メアドの一意のリスト | 
|     //************************************2017/08/29 DEL START [CHAN-AQN4FY]*********************************** | 
|     //Set<String> emails = new Set<String>(); | 
|      | 
|     //// メアド一覧を得る | 
|     //// insertの場合は、メアドが入っていれば全部対象 | 
|     //// updateの場合は、oldと newで値が違っている時だけ | 
|     //if ( Trigger.isInsert) { | 
|     //    for ( Lead target : Trigger.New) { | 
|     //        if ( target.Email != null) { | 
|     //            emails.add( target.Email); | 
|     //        } | 
|     //    } | 
|     //} | 
|     //else if ( Trigger.isUpdate) { | 
|     //    Lead leadOld = null, leadNew = null; | 
|     //    for ( Integer i = 0; i < Trigger.New.size(); i++) { | 
|     //        leadOld = Trigger.Old[i]; | 
|     //        leadNew = Trigger.New[i]; | 
|     //        if ( leadOld.email != leadNew.email && leadNew.email != null) { | 
|     //            emails.add( leadNew.email); | 
|     //        } | 
|     //    } | 
|     //} | 
|     //else { | 
|     //    return; | 
|     //} | 
|   | 
|     //// メアド一覧を元に contactを得る | 
|     //List<Contact> contacts = [SELECT Id, Email, OwnerId FROM Contact WHERE Email IN :emails ORDER BY Email, LastModifiedDate ASC]; | 
|     //Map<String, Contact> contactMap = new Map<String, Contact>(); | 
|     //for ( Contact contact : contacts) { | 
|     //    contactMap.put( contact.Email, contact); | 
|     //} | 
|      | 
|     //// 更新対象のリードに、contactへの参照をセットする | 
|     //Contact targetContact = null; | 
|     //for ( Lead targetLead : Trigger.New) { | 
|     //    targetContact = (Contact)contactMap.get( targetLead.Email); | 
|     //    if ( targetContact != null && targetLead.contact__c == null) { | 
|     //        targetLead.contact__c = targetContact.Id; | 
|     //        targetLead.OwnerId = targetContact.OwnerId; | 
|     //    } | 
|     //} | 
|     //************************************2017/08/29 DEL END [CHAN-AQN4FY]*********************************** | 
|     //************************************2017/08/29 ADD START [CHAN-AQN4FY]*********************************** | 
|     if ( Trigger.isInsert || Trigger.isUpdate) { | 
|         Set<String> departmentClass = new Set<String>(); | 
|         for ( Lead target : Trigger.New) { | 
|             departmentClass.add(target.Department_Class__c); | 
|         } | 
|   | 
|         List<Account> accounts = [SELECT Id, OwnerId FROM Account WHERE Id IN : departmentClass]; | 
|         Map<String, Account> accountMap = new Map<String, Account>(); | 
|         for (Account acc : accounts) { | 
|             accountMap.put(acc.Id, acc); | 
|         } | 
|   | 
|         Account targetAccount = null; | 
|         for ( Lead targetLead : Trigger.New) { | 
|             if (targetLead.owner_not_automatically_update__c == true) { | 
|                 if (Trigger.isInsert) { | 
|                     targetLead.OwnerId = UserInfo.getUserId(); | 
|                 } | 
|                 else { | 
|                     targetLead.OwnerId = targetLead.CreatedById; | 
|                 } | 
|             }  | 
|             else { | 
|                 targetAccount = (Account)accountMap.get(targetLead.Department_Class__c); | 
|                 if (targetAccount != null) { | 
|                     targetLead.OwnerId = targetAccount.OwnerId; | 
|                 } | 
|             } | 
|         } | 
|     } | 
|     //************************************2017/08/29 ADD END [CHAN-AQN4FY]*********************************** | 
| } |