function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DipthiDipthi 

Trigger on updating Account field value based on Contact - Struck plz suggest

Hi Awesome coders,
I have a checkbox field 'Out of Zip' on Account. When an Account's 'Billing Postal code' field is updated, check its Contacts 'mailing postal code'. If more than one contacts 'mailing postal code' field is not matching with Account then 'Out of Zip' field on Account should be turned True.

Plz correct my code in bold

trigger AccountOutOfZip on Account (After Update) {
    // Adding record Id's of Accounts whose postal code is updated
    Set<Id> AccIdSet = new Set<Id>() ;
    for(Account acc : Trigger.new) {
        Account oldAcc = Trigger.oldMap.get(acc.Id);
            If(acc.billingpostalcode  != oldAcc.billingpostalcode ) {
               AccIdSet.Add(acc.Id) ;
            }    
    }
    
    List <Account> AccUpdateList = New List<Account>();

    If(AccIdSet.size() > 0) {
        Account[] accList = [SELECT Id from Account where Id IN :AccIdSet] ; 
        Contact[] conList = [SELECT Id,AccountId from Contact where AccountId IN :AccIdSet] ;
        for(Account a : accList) {
            for(Contact c : conList) {
                If(a.billingpostalcode != c.MailingPostalCode) {
                  // a.Out_Of_Zip__c = True ;
                   AccUpdateList.add(a.Id = c.AccountId, a.Out_Of_Zip__c = True) ;

                }
            }
        }
     
    If(!AccUpdateList.isEmpty()) {
         update AccUpdateList ; 
    }
}    
    
AnudeepAnudeep (Salesforce Developers) 
Your logic looks correct. What issue are you facing?
DipthiDipthi

Hi Anudeep,

I am getting error on Line 20 :   AccUpdateList.add(a.Id = c.AccountId, a.Out_Of_Zip__c = True) ;

Error : Method does not exist or incorrect signature: void add(Id, Boolean) from the type List<Account>