Blog

Main » 2018 » January » 09
global class UpdateContactAddresses implements 
 Database.Batchable<sobject>, Database.Stateful {
 
 // instance member to retain state across transactions
 global Integer recordsProcessed = 0;

 global Database.QueryLocator start(Database.BatchableContext bc) {
 return Database.getQueryLocator(
 'SELECT ID, BillingStreet, BillingCity, BillingState, ' +
 'BillingPostalCode, (SELECT ID, MailingStreet, MailingCity, ' +
 'MailingState, MailingPostalCode FROM Contacts) FROM Account ' + 
 'Where BillingCountry = \'USA\''
 );
 }

 global void execute(Database.BatchableContext bc, List<account> scope){
 // process each batch of records
 List<contact> contacts = new List<contact>();
 for (Account account : scope) {
 for (Contact contact : account.contacts) {
 contact.MailingStreet = account.BillingStreet;
 contact.MailingCity = account.BillingCity;
 contact.MailingState = account.BillingState;
 contact.Mailing
		
		... 
		
			Read more »