Skip to content

Instantly share code, notes, and snippets.

@arun12209
Created October 12, 2022 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arun12209/fd22fcb6cae4029f45fc221fb604f8f1 to your computer and use it in GitHub Desktop.
Save arun12209/fd22fcb6cae4029f45fc221fb604f8f1 to your computer and use it in GitHub Desktop.
BatchUpdateContactMailingCity class to update the contact records
global with sharing class BatchUpdateContactMailingCity implements Database.Batchable<SObject>{
global Database.QueryLocator start(Database.BatchableContext bc){
String query = 'Select Id, BillingCity,(Select Id,Name, MailingCity from contacts) from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Account> accList){
//list of contacts to update
List<Contact> conListToUpdate = new List<Contact>();
for(Account acc: accList){
for(Contact con: acc.contacts){
con.MailingCity = acc.BillingCity;
conListToUpdate.add(con);
}
}
if(conListToUpdate.size()>0){
List<Database.SaveResult> updateResults = Database.update(conListToUpdate,false);
List<SObject> objList = new List<SObject>();
objList.addAll(conListToUpdate);
//call generic method to create the error log records
Util.createError(updateResults,objList,'Contact Update');
}
}
global void finish(Database.BatchableContext bc){
//add the finish code here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment