public without sharing class ProRegisterHandler { public static void updateProduct(List newList, Map newMap, List oldList, Map oldMap) { // 产品-注册证关系 中所有 产品ID List Product2_ID_list = new list(); // 现有产品 map Map Exist_ProductMap = new map(); // 需要更新产品 list List UpdateProductlist = new list(); // 根据变更的证ID找到关联的所有的产品 list ExistPRLList = [select Product2__c, Product_Register__c from Product_Register_Link__c where Product_Register__c in: newList ]; for (Product_Register_Link__c PRL : ExistPRLList) { // 产品ID的LIST Product2_ID_list.add(PRL.Product2__c); } // 通过产品ID找到该产品最新注册证ID Exist_ProductMap = new Map ([select ID, Register_Latest__c, registrationCode__c, clinical_product_code__c from Product2 where id in: Product2_ID_list ]); // 通过产品ID找到所有涉及的有效关系 List allPRLList = [select Product2__c, Product_Register__c, Product_Register__r.PrdCompanyAddr__c, Product_Register__r.ValidFrom__c, Product_Register__r.ValidTo__c, Product_Register__r.Name, Product_Register__r.ValidProductRegister__c, Product_Register__r.ClinicalProductCode__c from Product_Register_Link__c where Product2__c in : Product2_ID_list order by Product2__c, LastModifiedDate desc ]; String tmpProductID = ''; Product2 temProduct = new Product2(); Map UpdProMap = new Map(); for (Product_Register_Link__c temPRL : allPRLList) { if (!tmpProductID.equals(temPRL.Product2__c)) { tmpProductID = temPRL.Product2__c; temProduct = Exist_ProductMap.get(tmpProductID); temProduct.Register_Latest__c = temPRL.Product_Register__c; // 最新注册证ID temProduct.registrationCode__c = null; temProduct.clinical_product_code__c = null; } // 如果注册证状态有效,记录有效注册证号 if (temPRL.Product_Register__r.ValidProductRegister__c ) { if (String.isBlank(temProduct.registrationCode__c)) { temProduct.registrationCode__c = temPRL.Product_Register__r.Name + '\n'; } else { if (String.isNotBlank(temPRL.Product_Register__r.Name)) { if (temProduct.registrationCode__c.indexOf(temPRL.Product_Register__r.Name) == -1) { temProduct.registrationCode__c += temPRL.Product_Register__r.Name + '\n'; } } } String clinicalCode = temPRL.Product_Register__r.ClinicalProductCode__c; if (String.isBlank(temProduct.clinical_product_code__c)) { temProduct.clinical_product_code__c = clinicalCode;//temPRL.Product_Register__r.ClinicalProductCode__c; } else { if (String.isNotBlank(clinicalCode)) { Map codeMap = new Map(); for (String code : temProduct.clinical_product_code__c.split('/')) { codeMap.put(code, code); } codeMap.put(clinicalCode, clinicalCode); temProduct.clinical_product_code__c = ''; for (String code : codeMap.values()) { if (String.isBlank(temProduct.clinical_product_code__c)) { temProduct.clinical_product_code__c = code; } else { temProduct.clinical_product_code__c += '/' + code; } } } } } UpdProMap.put(tmpProductID, temProduct); } if (UpdProMap.keySet().size() > 0) update UpdProMap.values(); } //用于降类,产品注册证更新了,就把产品注册证下的产品的等级类别字段赋空,触发产品的触发器 public static void checkRegistervervaldatum(List newList, Map newMap, List oldList, Map oldMap) { List prcList = new List(); for (Product_Register__c pr : newList) { Product_Register__c oldpr = oldMap.get(pr.Id); prcList.add(pr.id); } //放到Map里,用于注册证更新产品 Map prt2Map = new Map(); //通过之前存储的Id用于关系表的查询。 List prlList = new List(); prlList = [select Product2__r.id,Product2__r.demoteer_Sap__c,Product2__r.Diedatvanink__c,Product_Register__r.MedPrdClass__c,Product_Register__r.Stelsedag__c,Product_Register__r.ValidTo__c,Product_Register__r.ValidFrom__c from Product_Register_Link__c where Product_Register__r.Id in : prcList]; List proIdList = new List(); for (Product_Register_Link__c prl: prlList) { proIdList.add(prl.Product2__r.id); } List por2Lsit = new List(); //查出产品的id和等级类别的字段 por2Lsit = [select Id,Level_Category__c from Product2 where Id in : proIdList]; for (Product2 pro: por2Lsit) { pro.Level_Category__c = null; prt2Map.put(pro.Id, pro); } System.debug('prt2Map='+prt2Map); if (prt2Map.size() > 0) { update prt2Map.values(); } } }