| trigger ISOppoDemand_Trigger on IS_Opportunity_Demand__c (before insert,before update,after insert,after update) { | 
|     boolean UpdateOppor{get;set;} | 
|     List<Id> copyUserIds = new List<Id>(); | 
|     List<IS_Opportunity_Demand__c> newList = new List<IS_Opportunity_Demand__c>(); | 
|     if (Trigger.isInsert) { | 
|         if(Trigger.isBefore){ | 
|   | 
|             Map<id,Integer> isoCountMap = new Map<id,Integer>(); | 
|             isoCountMap = ISO_DemandOperAndDemonsController.setNameForNew(Trigger.new); | 
|             system.debug(isoCountMap); | 
|             for(IS_Opportunity_Demand__c iso : Trigger.new){ | 
|                 iso.Opportunity_sub_owner__c = iso.Opportunity_sub_ownerID__c; | 
|                 iso.Name_Index__c = 1+(isoCountMap.get(iso.Opportunity_ID__c)==null?0:(isoCountMap.get(iso.Opportunity_ID__c))); | 
|                 isoCountMap.get(iso.Opportunity_ID__c); | 
|             } | 
|         } | 
|         if(Trigger.isAfter){ | 
|         } | 
|     } | 
|     if (Trigger.isUpdate) { | 
|          | 
|         if(Trigger.isAfter){ | 
|             UpdateOppor = ISO_DemandOperAndDemonsController.UpdateMotherOpportunity(Trigger.old,Trigger.new); | 
|         } | 
|     } | 
|     if(Trigger.isBefore&&(Trigger.isInsert||trigger.isUpdate)){ | 
|             IS_Opportunity_Demand__c old = null; | 
|         if(Trigger.oldMap!=null){ | 
|             for(IS_Opportunity_Demand__c iso: Trigger.new){ | 
|                 old = Trigger.oldMap.get(iso.Id); | 
|                 if (Trigger.isInsert | 
|                             || (Trigger.isUpdate && old.Submit_Date__c != iso.Submit_Date__c && iso.Func_SOD_Status__c == '02 已提交') | 
|                             || (Trigger.isUpdate && old.OwnerId != iso.OwnerId) | 
|                     ) { | 
|                         newList.add(iso); | 
|                         //if (Trigger.isInsert || (Trigger.isUpdate && old.applyUser__c != iso.applyUser__c)) { | 
|                         copyUserIds.add(iso.OwnerId); | 
|                         //} else { | 
|                         //    copyUserIds.add(UserInfo.getUserId()); | 
|                         //} | 
|                     } | 
|             } | 
|         }else{ | 
|             for(IS_Opportunity_Demand__c iso: Trigger.new){ | 
|                 newList.add(iso); | 
|                 //if (Trigger.isInsert || (Trigger.isUpdate && old.applyUser__c != iso.applyUser__c)) { | 
|                 copyUserIds.add(iso.OwnerId); | 
|             } | 
|         } | 
|          | 
|         if (copyUserIds.size() > 0) { | 
|                 Map<Id, User> copyUserMap = new Map<Id, User>([ | 
|                     SELECT Id, Name, SalesManager__c, BuchangApprovalManagerSales__c, JingliApprovalManager__c, BuchangApprovalManager__c, ZongjianApprovalManager__c FROM User WHERE Id IN :copyUserIds | 
|                 ]); | 
|                  | 
|                 for (Integer i = 0; i < copyUserIds.size(); i++) { | 
|                     IS_Opportunity_Demand__c local = newList[i]; | 
|                     User loginUser = copyUserMap.get(copyUserIds[i]); | 
|                     local.SI_SalesManager__c = loginUser.SalesManager__c == null ? loginUser.Id : loginUser.SalesManager__c; | 
|                     local.SI_Minister_Sell__c = loginUser.BuchangApprovalManagerSales__c == null ? loginUser.Id : loginUser.BuchangApprovalManagerSales__c; | 
|                     local.SI_Minister_Service__c = loginUser.BuchangApprovalManager__c == null ? loginUser.Id : loginUser.BuchangApprovalManager__c; | 
|                     local.SI_Majordomo__c = loginUser.ZongjianApprovalManager__c == null ? loginUser.Id : loginUser.ZongjianApprovalManager__c; | 
|                 } | 
|         } | 
|     } | 
|   | 
| } |