0

I am trying to do a mass update of all account email fields 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');
    }
}
4
  • 1
    Generally speaking, if you want to access a value on an SObject record (as you're currently doing with the Email field), you need to query for it. Providing the exact error message (don't paraphrase it, you'll end up omitting the important parts) also helps.
    – Derek F
    Jun 26 at 22:06
  • Hey Derek, the error is on s.email = s.email + ' Testing.com'; Error: Compile Error: Variable does not exist: s at line 10 column 23 Jun 26 at 22:15
  • 1
    Is your loop variable in your actual code 's', or is it 'ac'? Also, like I said earlier, you need to query for a field if you want to use it in the right hand side of an expression.
    – Derek F
    Jun 26 at 22:20
  • 4
    In addition to Derek's point about the query, there is no email field on the Account object, see documentation. Did you create a custom field on Account to track email? Please edit your post to include more details about your cnofiguration. Jun 26 at 23:19
1

There is no standard field email on Account Object unless you are using a person account.

If you are using person account then the field name is PersonEmail.

ac.PersonEmail = ac.PersonEmail + ' Testing.com';

If email is a custom field on your Account object then the api name will be of the form email__c

ac.Email__c = ac.Email__c + ' Testing.com';

The above will resolve the compile error but you will still get a runtime error if you do not include the field in SOQL query in start method.

0

The error is on line return Database.getQueryLocator('SELECT name from Account'); since you have not added your email field while doing the query.

Change the query as below.

return Database.getQueryLocator('SELECT name, email from Account');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.