function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rick RossiRick Rossi 

Batch Class Variable Question

Hello!

I am trying to do a mass update of all account emails to "testing.com"
however I am getting an error message saying my variable does not exist. This is my class.

global class BatchUpdateAccountField implements Database.Batchable <SObject>{
global Database.QueryLocator start (Database.BatchableContext bc)
{
    return Database.getQueryLocator('SELECT name from Account');
}
   global void execute(Database.BatchableContext bc, List<Account> acList)
   {
       for(Account ac :acList)
       {
          ac.email = ac.email + ' Testing.com';
           
       }
       
       update acList;
   }
    
    global void finish(Database.BatchableContext bc)
    {
        System.debug('>>>Finish');
    }
    
}


Any help would be great!
Suraj Tripathi 47Suraj Tripathi 47
Hi Rick,

You are getting this error because there is no standard email field in account object. First create custom email field then you can take reference from this below code and you can change the execute method as you like:-
global class BatchUpdateAccountField implements Database.Batchable <SObject>{
    global Database.QueryLocator start (Database.BatchableContext bc)
    {
        return Database.getQueryLocator('SELECT name,email__c from Account');
    }
    global void execute(Database.BatchableContext bc, List<Account> acList)
    {
        for(Account ac :acList)
        {
            if(ac.email__c!=null){
                ac.email__c = ac.email__c + 'Testing.com';
            }
            else{
                ac.email__c = ac.email__c + '@Testing.com';
            }
        }
        if(acList.size()>0){
            update acList;
        }
    }
    
    global void finish(Database.BatchableContext bc)
    {
        System.debug('>>>Finish');
    }
    
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
AbhinavAbhinav (Salesforce Developers) 
Hi,

If you are using any other field also in execute method then you have to include that field as well in query to avoid this type of error.

Thanks!