1
帳戶和聯繫人都有Billing_Address__c字段。聯繫人也有複選框稱爲active__c。如果active__c爲true並且Account Billing_Address__c更新,則更新Contact的Billing_Address__c。這是觸發器。它工作正常。但是我想知道是否有任何問題,或者如何根據內存條件對此進行優化?觸發帳戶更新聯繫人
public static void updateContactBillingAddress(List<Account> lstNew, Map<Id,Account> mapOld){
Set<Id> accIds = new Set<Id>();
for(Account acc : lstNew){
if(acc.Billing_Address__c != mapOld.get(acc.Id).Billing_Address__c && acc.Billing_Address__c !=null){
accIds.add(acc.Id);
}
}
if(!accIds.isEmpty()){
List<Contact> lstContact = new List<Contact>([Select ID,active__c, Account.Billing_Address__c,Billing_Address__c FROM Contact where AccountID IN :accIds]);
List<Contact> lstUpdateCon = new List<Contact>();
for(Contact con : lstContact){
if(con.active__c == true){
if(con.Billing_Address__c != con.Account.Billing_Address__c){
con.Billing_Address__c = con.Account.Billing_Address__c;
lstUpdateCon.add(con);
}
}
else{
con.Billing_Address__c =null;
lstUpdateCon.add(con);
}
}
if(!lstUpdateCon.isEmpty()){
update lstUpdateCon;
}
}
}
的可能的複製[觸發帳戶更新聯繫人字段(http://stackoverflow.com/questions/43397554/trigger-on-account-to-update-contact-field) – Jaiman