I need to do one time data update in the org. I plan to do this using batch apex. As this is one time update, I don't want to move batch class to production and don't want to write apex unit tests for it.
I am trying to define batch class in anonymous apex and call it from anonymous apex. Overall structure of anonymous apex is as follows -
public class batchsample implements Database.Batchable<sObject> {
public string query;
public Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List<sObject> Scope)
{
List<customobj__c> ListToUpdate = new List<customobj__c>();
for(sobject s: Scope)
{
customobj__c m1 = (customobj__c)s;
//update some fields here
ListToUpdate.add(m1);
}
if(ListToUpdate.size() > 0)
update ListToUpdate;
}
public void finish(Database.BatchableContext BC)
{
//send email
}
}
batchsample startbatch = new batchsample();
startbatch.query = SOQL query goes here
System.scheduleBatch(startbatch, 'samplebatch', 1, 100);
But when I execute above code as anonymous apex, I get an error
Error - Only top-level classes can implement Database.Batchable
Can someone help me understand why this error is given ? And if there is a way to call batch apex from anonymous apex where Batch Class definition is also part of anonymous apex code ?