+ Start a Discussion
Deepak Sharma 184Deepak 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);
        }
        }
    
        
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
global class UpdateContactAdresses implements Database.Batchable<sObject>, Database.stateful

{
    global Static 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);
        }
}
Let us know if this will help you
 
Harish RamachandruniHarish Ramachandruni
Hi check your code use below smal changes in down in your code check this bellow code .

i ma shore it will execute .
 
global class UpdateContactAdresses implements Database.Batchable<sObject>, Database.stateful

{
    global Integer recordsProcessed = 0;
    
    global Database.QueryLocator start(Database.BatchableContext bc)
        
    {
       string query = 'select id, BillingStreet, BillingCity, BillingState, '+'BillingPostalCode, (select id, MailingStreet, MailingCity,'+' MailingState, MailingPostalCode from contacts) from Account'+'where BillingCountry=\'USA\'' ;
        
            
            
              return Database.getQueryLocator(query);

        
    }
    
    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(recordsProcessed +' record is processed');
         AsyncApexJob job = [select id, status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where id=: bc.getJobId()];
        // EmailUtils.SendMessage(a, recordsProcessed);
        }
        }



If it is usefull make it best answer .

Regards,

harish.R.

Pawani MutturuPawani Mutturu
Hi,

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');
Pawani MutturuPawani Mutturu
Hi again,
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!
Deepak Sharma 184Deepak Sharma 184
Thanks pawani, how to resolve the error of utility class. means how to call that class. it would be grateful.