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