You need to sign in to do that
Don't have an account?
Babar 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 :
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 :
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:
If this information helps, please mark the answer as best. Thank you
All Answers
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:
If this information helps, please mark the answer as best. Thank you
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:
I have changed :
Please mark this comment as best answer if it's help you.