public without sharing class AssetGuaranteeTimeChangeHandler extends Oly_TriggerHandler{ private Map newMap; private Map oldMap; private List newList; private List oldList; private List idList; private List assetList; private List statu_AchievementsIds; public AssetGuaranteeTimeChangeHandler(){ this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; this.idList = new List(); this.assetList = new List(); this.statu_AchievementsIds = new List(); } protected override void beforeInsert() { ambChange_Account(); } protected override void beforeUpdate(){ ambChange_Account(); updateambChange_Account(); } //给"保修期变更申请"的所有人相关字段赋值,用于审批流 private void ambChange_Account(){ List ownerIds = new List(); for(GuaranteePeriodAlterationApplication__c gpaa :newList){ ownerIds.add(gpaa.OwnerId); } User ownerUser = null; if (ownerUser == null ) { ownerUser = [SELECT Id, Name, SalesManager__c, JingliApprovalManager__c, BuchangApprovalManager__c, BuchangApprovalManagerSales__c, ZongjianApprovalManager__c, TongkuoZongjian__c FROM User WHERE Id = :ownerIds]; } for(GuaranteePeriodAlterationApplication__c gpaa :newList){ gpaa.OwnerManager__c = ownerUser.SalesManager__c ; gpaa.OwnerSectionChief__c = ownerUser.BuchangApprovalManager__c; gpaa.OwnerMajordomo__c = ownerUser.ZongjianApprovalManager__c; } } //审批通过将保修期限赋值给保有设备 private void updateambChange_Account() { //List statu_AchievementsIds = new List(); Map updateStatu_AchievementsMap = new Map(); List statu_AchievementsList = new List(); for(GuaranteePeriodAlterationApplication__c gpaa :newList){ if(oldMap.get(gpaa.Id).get('Change_status__c') != gpaa.Change_status__c && gpaa.Change_status__c == '批准'){ idList.add(gpaa.Id); statu_AchievementsIds.add(gpaa.GpaaStatu_Achievements__c); updateStatu_AchievementsMap.put(gpaa.GpaaStatu_Achievements__c,gpaa); } } if (statu_AchievementsIds != null && statu_AchievementsIds.size() > 0) { for (String aId : statu_AchievementsIds) { Statu_Achievements__c statu_Achievements = new Statu_Achievements__c(); if (updateStatu_AchievementsMap.containsKey(aId)) { statu_Achievements.Id = aId; statu_Achievements.ChangeWarranty__c = true; if (String.isBlank(updateStatu_AchievementsMap.get(aId).ChangeReason_text__c)) { statu_Achievements.ChangeWarrantyReason__c = updateStatu_AchievementsMap.get(aId).ChangeReason__c; } else { statu_Achievements.ChangeWarrantyReason__c = updateStatu_AchievementsMap.get(aId).ChangeReason_text__c; } statu_AchievementsList.add(statu_Achievements); } } if (statu_AchievementsList != null && statu_AchievementsList.size() > 0) { update statu_AchievementsList; } } if(idList != null && idList.size() > 0){ List ambcDetailList ; ambcDetailList = [select Id,Asset__c,GuaranteeTime__c,Asset__r.guarantee_period__c, GuaranteePeriodAlteration__r.WarrantyStartDate__c //GuaranteePeriodAlteration__r.GuaranteeTime__c from GuaranteePeriodAlterationDetail__c where GuaranteePeriodAlteration__c In :idList]; for (GuaranteePeriodAlterationDetail__c gpaDetail :ambcDetailList){ Asset asset = new Asset(); asset.Id = gpaDetail.Asset__c; asset.WarrantyChanges__c = true; asset.Guarantee_period_for_products__c = gpaDetail.GuaranteeTime__c; asset.ChangeWarrantyStartDate__c = gpaDetail.GuaranteePeriodAlteration__r.WarrantyStartDate__c; asset.InstallDate = gpaDetail.GuaranteePeriodAlteration__r.WarrantyStartDate__c; assetList.add(asset); } } if(assetList != null && assetList.size() > 0 ){ update assetList; } } }