Skip to main content
Is it possible to use the batchable class in an anonymous block?

I'm attempting to update to a few hundred accounts from within the Execute Anonymous environment, but because of the existing Apex, Im getting "Too many SOQL queries: 50001".

So I attempted the following code (still within an  anonymous block) and it will come back with 

"Execute Anonymous Error

Line: 13, Column: 14

Global type must be contained inside of a global class"

I think the developer console doesn't like global classes...    Does it ?

Do I absolutely need a global modifier to use the SFDC batchable classes ?

Advise will be appreciated

This is the actual code:  

global class SICS implements Database.Batchable<Account>{

   global final String Query;

   string q = 'Select Id, CMSIC8A__c, Temp_BadCMSIC__c from Account' +

               'where Temp_BadCMSIC__c != NULL ';

    

  global UpdateAccountFields(String q){ Query=q;}

  global Database.querylocator start(Database.BatchableContext BC){

            return Database.getQueryLocator(query);}

    

 global void execute(Database.BatchableContext BC, List<Account> scope){

   

LIST<Account> SIC2Up  = [Select Id, CMSIC8A__c, Temp_BadCMSIC__c from Account where Temp_BadCMSIC__c != ''];

List <Account> Acc2Up = new List<Account>();

    for(Account acc : scope){

    IF(acc.Temp_BadCMSIC__c != NULL){

       acc.CMSIC8A__c = acc.Temp_BadCMSIC__c;

    }

    Acc2Up.add(acc);

}

Update Acc2Up;

}

}

// SICS obj = new SICS(); 

Id batchJobId = Database.executeBatch(new SICS(),200);

Thanks

RSM

 
7 respuestas
  1. 28 jun 2017, 22:36

    I just updated your Batch job like below and also tested in org

    global class SICS implements Database.Batchable<sObject>

    {

    global final String Query;

    string q = 'Select Id, CMSIC8A__c, Temp_BadCMSIC__c from Account where Temp_BadCMSIC__c != NULL ';

    global SICS()

    {

    Query=q;

    }

    global SICS(String q)

    {

    Query=q;

    }

    global Database.querylocator start(Database.BatchableContext BC)

    {

    return Database.getQueryLocator(query);

    }

    global void execute(Database.BatchableContext BC, List<Account> scope)

    {

    List <Account> Acc2Up = new List<Account>();

    for(Account acc : scope)

    {

    /*

    IF(acc.Temp_BadCMSIC__c != NULL)

    {

    acc.CMSIC8A__c = acc.Temp_BadCMSIC__c;

    Acc2Up.add(acc);

    }

    */

    }

    if(Acc2Up.size() > 0 )

    {

    Update Acc2Up;

    }

    }

    global void finish(Database.BatchableContext BC) {

    }

    }

    Then execute below code form your anonymous block

    Id batchJobId = Database.executeBatch(new SICS(),200);

    Let us know if this will help you

     
0/9000