| trigger AssetRecordTypeUpd on Asset (before insert, before update, before delete) { | 
|     //add by gzw 20210729 start | 
|     if (StaticParameter.EscapeVMCTrigger) { | 
|         return ; | 
|     } | 
|     //add by gzw 20210729 end | 
|     List<String> cdList = new List<String>(); | 
|     if (trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) { | 
|         for(Asset ass : Trigger.new) { | 
|             ass.RecordTypeID__c = ass.RecordTypeId; | 
|             // 注残设置 | 
|             if (!String.isBlank(ass.Order_No__c) && ass.Backorder__c == null) { | 
|                 cdList.add(ass.Order_No__c); | 
|             } | 
|   | 
|             // HWAG-AWLAU6 2018/3/7 "是否多年保修"的值拷贝到"是否多年保修(文本)"中 | 
|             if (trigger.isUpdate && Trigger.oldMap.get(ass.Id).IS_Extend_Gurantee_Txt__c != ass.IS_Extend_Gurantee_Txt__c) { | 
|                 ass.Extend_Gurantee_Txt_Manual__c = true; | 
|             } else { | 
|                 if (ass.IS_Extend_Gurantee__c != ass.IS_Extend_Gurantee_Txt__c && ass.Extend_Gurantee_Txt_Manual__c == false) { | 
|                     ass.IS_Extend_Gurantee_Txt__c = ass.IS_Extend_Gurantee__c; | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     if (cdList.size() > 0) { | 
|         List<Statu_Achievements__c> saList = [select Id, Name, ContractNO__c from Statu_Achievements__c where ContractNO__c IN : cdList AND Valid_Statu_Achievements__c = 1]; | 
|         if (saList != null && saList.size() > 0) { | 
|             Map<String, String> saMap = new Map<String, String>(); | 
|             for (Statu_Achievements__c sa : saList) { | 
|                 saMap.put(sa.ContractNO__c, sa.Id); | 
|             } | 
|             for (Asset a : Trigger.new) { | 
|                 for (String s : saMap.keySet()) { | 
|                     if (a.Order_No__c == s) { | 
|                         a.Backorder__c = saMap.get(s); | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
| //********** HWAG-AXVBVV ********* 2018/4/17 clj ADD START ************************* | 
|     if (trigger.isBefore && trigger.isDelete) { | 
|         cdList = new List<String>(); | 
|         Map<String, String> delMap = new Map<String, String>(); | 
|         for (Asset delId : Trigger.old) { | 
|             cdList.add(delId.Id); | 
|         } | 
|         if (cdList.size() > 0) { | 
|             List<AggregateResult> mcList = [select Max(Id) Id, Asset__c  | 
|                         from Maintenance_Contract_Asset__c  where Asset__c In : cdList group by Asset__c]; | 
|             List<AggregateResult> reList = [select Max(id) Id, Delivered_Product__c from Repair__c  | 
|                         where Status1__c != '0.删除' and Delivered_Product__c In : cdList group by Delivered_Product__c]; | 
|             List<AggregateResult> qisList = [select Max(Id) Id, nonyushohin__c from QIS_Report__c  | 
|                         where nonyushohin__c In : cdList group by nonyushohin__c]; | 
|             if (mcList != null && mcList.size() > 0) { | 
|                 for (AggregateResult mc : mcList) { | 
|                     delMap.put(String.valueOf(mc.get('Asset__c')), String.valueOf(mc.get('Asset__c'))); | 
|                 } | 
|             } | 
|             if (reList != null && reList.size() > 0) { | 
|                 for (AggregateResult re : reList) { | 
|                     delMap.put(String.valueOf(re.get('Delivered_Product__c')), String.valueOf(re.get('Delivered_Product__c'))); | 
|                 } | 
|             } | 
|             if (qisList != null && qisList.size() > 0) { | 
|                 for (AggregateResult qis : qisList) { | 
|                     delMap.put(String.valueOf(qis.get('nonyushohin__c')), String.valueOf(qis.get('nonyushohin__c'))); | 
|                 } | 
|             } | 
|         } | 
|         for (Asset adel : Trigger.old) { | 
|             if (delMap.containsKey(adel.Id)) { | 
|                  adel.addError('该设备存在维修合同、修理或QIS,不能删除。'); | 
|             } | 
|         } | 
|     } | 
| //********** HWAG-AXVBVV ********* ADD END ***************************************** | 
| } |