public without sharing class OrderItemTriggerHandler { public static void setProductConfig(List newList, Map newMap, List oldList, Map oldMap) { List targetList = null; if (Trigger.isDelete) { targetList = oldList; } else { targetList = newList; } // 处理对象的合同ID List ordIdList = new List(); Map ordMap = new Map(); for (OrderItem target : targetList) { if (ordMap.containsKey(target.OrderId) == false) { ordIdList.add(target.OrderId); ordMap.put(target.OrderId, target.OrderId); } } // 取得处理对象的合同产品 List oiList = [select Id, PricebookEntry.Product2.ProductCode, Quantity, OrderId from OrderItem where OrderId = :ordIdList order by OrderId, PricebookEntry.Product2.ProductCode, Quantity]; Map> oiMap = new Map>(); List nList = new List(); String tmp = ''; for (OrderItem oi : oiList) { if (tmp == '' || oi.OrderId != tmp) { tmp = oi.OrderId; nList = new List(); oiMap.put(oi.OrderId, nList); nList.add(oi); } else { nList.add(oi); } } // 更新处理对象的合同 List ordList = [select Id, RecordTypeId, ProductConfig__c, ProductConfig_D__c from Order where Id = :ordIdList]; List updList = new List(); for (Order target : ordList) { List ois = oiMap.get(target.Id); if (ois == null || ois.size() == 0) { continue; } String oiStr = ''; for (OrderItem oi : ois) { if (oiStr == '') { oiStr += oi.PricebookEntry.Product2.ProductCode + '*' + oi.Quantity + '\n'; } else { oiStr += oi.PricebookEntry.Product2.ProductCode + '*' + oi.Quantity + '\n'; } } boolean individualCase = target.RecordTypeId == System.Label.RT_Contract_IndividualCase_ANI || target.RecordTypeId == System.Label.RT_Contract_IndividualCase_BS || target.RecordTypeId == System.Label.RT_Contract_IndividualCase_IE || target.RecordTypeId == System.Label.RT_Contract_IndividualCase_NDT || target.RecordTypeId == System.Label.RT_Contract_IndividualCase_RVI; Order upd = new Order(Id = target.Id); if (UserInfo.getUserType() == 'PowerPartner' || target.RecordTypeId == System.Label.RT_ServiceContract || target.RecordTypeId == System.Label.RT_ServiceContractIE || target.RecordTypeId == System.Label.RT_ServiceContractRVI || individualCase) { upd.ProductConfig_D__c = oiStr; } else { upd.ProductConfig__c = oiStr; upd.ProductConfig_D__c = oiStr; } updList.add(upd); } if (updList.size() > 0) update updList; } public static void insSingleProduct(List newList, Map newMap, List oldList, Map oldMap) { List idList = new List(); idList.addAll(newMap.keySet()); List ospList = new List(); if(idList.size() >0 ){ for(Id id: idList){ if(newMap.get(id).ODISingleProduct__c == true){ order_Single_product__c osp = new order_Single_product__c(); osp.OrderProduct__c = id; osp.Order__c = newMap.get(id).OrderId; osp.CurrencyIsoCode = newMap.get(id).CurrencyCode__c; ospList.add(osp); } } System.debug(ospList); insert ospList; } } public static void delSingleProduct(List newList, Map newMap, List oldList, Map oldMap) { List idList = new List(); for(OrderItem ord : oldList){ if(ord.ODISingleProduct__c == true){ idList.add(ord.Id); } } if(idList.size() >0 ){ List ospList = [select id from order_Single_product__c where OrderProduct__c in :idList]; System.debug('ospList***'+ospList); delete ospList; } } public static void tess(){ Integer i = 0; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; } }