You need to sign in to do that
Don't have an account?
Deepak Sharma 184
Getting an error while doing batch apex class"Variable does not exist: recordProcessed", but already defined in the class.
global class UpdateContactAdresses implements Database.Batchable<sObject>, Database.stateful
{
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)
{
list<contact> contacts= new list<contact>();
for(Account account: scope)
{
for(Contact contact:account.contacts)
{
contact.MailingStreet = account.BillingStreet;
contact.MailingState = account.BillingState;
contact.MailingCity = account.BillingCity;
contact.MailingPostalCode = account.BillingPostalCode;
contacts.add(contact);
recordsProcessed = recordsProcessed + 1;
}
}
update contacts;
}
global void finish(Database.BatchableContext bc)
{
system.debug(recordProcessed +' record is processed');
AsyncApexJob job = [select id, status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where id=: bc.getJobId()];
EmailUtils.SendMessage(a, recordsProcessed);
}
}
{
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)
{
list<contact> contacts= new list<contact>();
for(Account account: scope)
{
for(Contact contact:account.contacts)
{
contact.MailingStreet = account.BillingStreet;
contact.MailingState = account.BillingState;
contact.MailingCity = account.BillingCity;
contact.MailingPostalCode = account.BillingPostalCode;
contacts.add(contact);
recordsProcessed = recordsProcessed + 1;
}
}
update contacts;
}
global void finish(Database.BatchableContext bc)
{
system.debug(recordProcessed +' record is processed');
AsyncApexJob job = [select id, status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where id=: bc.getJobId()];
EmailUtils.SendMessage(a, recordsProcessed);
}
}
i ma shore it will execute .
If it is usefull make it best answer .
Regards,
harish.R.
While declaring the variable you have declared as 'recordsProcessed'.
But in the "Finish" method you are using it as 'recordProcessed' :-
system.debug(recordProcessed +' record is processed');
Change the above statement to system.debug(recordsProcessed +' record is processed');
The other change that you need to do is :-
Change this :- return.Database.getQueryLocator('select id, BillingStreet, BillingCity, BillingState, '+'BillingPostalCode, (select id, MailingStreet, MailingCity,'+' MailingState, MailingPostalCode from contacts) from Account'+'where BillingCountry=\'USA\'');
To this :- return Database.getQueryLocator('select id, BillingStreet, BillingCity, BillingState, '+'BillingPostalCode, (select id, MailingStreet, MailingCity,'+' MailingState, MailingPostalCode from contacts) from Account'+'where BillingCountry=\'USA\'');
There is no dot between return and Database.
Also you may get an error in EmailUtils.SendMessage(a, recordsProcessed); because EmailUtils in a utility class. You should have that class in your org before you call it in this class.
Hope I was clear!