+ Start a Discussion
BrianWXBrianWX 

COMPILE ERROR: Variable is not visible

I have this Batch Apex codes out Salesforce help page.  I tried it and I kept getting this error:

COMPILE ERROR: Variable is not visible: query
LINE: 3 COLUMN: 1

 

From the codes standpoint, I am not sure where the codes missed.

 

Here is the Apex class:

 

global class OwnerReassignment implements Database.Batchable<sObject>{
String query;
String email;
Id toId;
Id fromId;

global database.querylocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);}

global void execute(Database.BatchableContext BC, List<sObject> scope){
    List<Account> accns = new List<Account>();

   for(sObject s : scope){Account a = (Account)s;
        if(a.Id==fromId){
            a.Rating = 'Warm';
            accns.add(a);
            }
        }

update accns;
   
}
global void finish(Database.BatchableContext BC){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setToAddresses(new String[] {email});
mail.setReplyTo('briandowx@yahoo.com');
mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Batch Process Completed');
mail.setPlainTextBody('Batch Process has completed');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

 

Here is the Apex execute codes that I used to run in Workbench or System Log:

 

id u = '0017000000MHEYd';
OwnerReassignment reassign = new OwnerReassignment();
reassign.query = 'SELECT Id, Name, OwnerId FROM Account WHERE Id=\'' + u + '\'';
reassign.email = 'admin@acme.com';
reassign.fromId = '0017000000MHEYd';
reassign.toId = '0017000000MHEYd';
ID batchprocessid = Database.executeBatch(reassign);

 

Thank you in advance your help.

Brian


Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Check with defining the Variable as global.

global final String query;

 

 

All Answers

Rahul SharmaRahul Sharma

Check with defining the Variable as global.

global final String query;

 

 

This was selected as the best answer
Sridhar BonagiriSridhar Bonagiri

Hi,

 

Please declare the required variable as global.