Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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