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
Babar Hussain 13Babar Hussain 13 

update related contacts

HI Team, 

I have one task updatevthe contacts related to account. I have wrote the apex code but getting error , please check and let me know where I'm making mistake. 

Apex code :

public class updateRelatedAcc {
    public static  void accUpdate (List<Account> accList){
        Set<Id> getUpdateAcc = new Set<Id>();
        for(Account a: accList) {
            getUpdateAcc.add(a.Id);
            
        }
        
        List<Contact> con = [Select Id, Name,AccountId, MailingCity from Contact Where AccountId =: getUpdateAcc];
        List<Contact> updateContacts = new List<Contact>();
        for(Contact c :con) {
            c.MailingCity = a.BillingCity;
            updateContacts.add(c);
        }
        update updateContacts;
        

    }

}

Error Screenshot :

Error
Best Answer chosen by Babar Hussain 13
SwethaSwetha (Salesforce Developers) 
HI Babar,
Based on the code provided,the variable "a" is not defined in the second for loop. It should be replaced with "accList" to reference the input parameter.

Updated code:
public class updateRelatedAcc {
    public static void accUpdate(List<Account> accList) {
        Set<Id> getUpdateAcc = new Set<Id>();
        for(Account a : accList) {
            getUpdateAcc.add(a.Id);
        }
        
        List<Contact> con = [SELECT Id, Name, AccountId, MailingCity, Account.BillingCity FROM Contact WHERE AccountId IN :getUpdateAcc LIMIT 50000];
        List<Contact> updateContacts = new List<Contact>();
        for(Contact c : con) {
            c.MailingCity = c.Account.BillingCity;
            updateContacts.add(c);
        }
        update updateContacts;
    }
}

If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Babar,
Based on the code provided,the variable "a" is not defined in the second for loop. It should be replaced with "accList" to reference the input parameter.

Updated code:
public class updateRelatedAcc {
    public static void accUpdate(List<Account> accList) {
        Set<Id> getUpdateAcc = new Set<Id>();
        for(Account a : accList) {
            getUpdateAcc.add(a.Id);
        }
        
        List<Contact> con = [SELECT Id, Name, AccountId, MailingCity, Account.BillingCity FROM Contact WHERE AccountId IN :getUpdateAcc LIMIT 50000];
        List<Contact> updateContacts = new List<Contact>();
        for(Contact c : con) {
            c.MailingCity = c.Account.BillingCity;
            updateContacts.add(c);
        }
        update updateContacts;
    }
}

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
Julien SalensonJulien Salenson
Hi babar,

The error you're encountering, "variable does not exist: a," is because the variable 'a' is not defined within the scope of your second for loop where you're trying to update the MailingCity field of the Contact records. In this context, 'a' does not have any meaning, and that's why you're getting the error.

Here's a corrected version of your Apex code:
public class updateRelatedAcc {
    public static void accUpdate(List<Account> accList) {
        Set<Id> getUpdateAcc = new Set<Id>();
        for (Account a : accList) {
            getUpdateAcc.add(a.Id);
        }
        
        List<Contact> con = [SELECT Id, Name, AccountId, MailingCity FROM Contact WHERE AccountId IN :getUpdateAcc];
        List<Contact> updateContacts = new List<Contact>();
        
        for (Contact c : con) {
            for (Account a : accList) {
                if (c.AccountId == a.Id) {
                    c.MailingCity = a.BillingCity;
                    updateContacts.add(c);
                }
            }
        }
        
        update updateContacts;
    }
}

I have changed :
  • the query to filter Contact records based on the AccountId using IN
  • added a another for loop to match the Contact records with the corresponding Account records by comparing AccountId.

Please mark this comment as best answer if it's help you.