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
fiona gentryfiona gentry 

How To Execute Below code In Anonymous Block

Dear Gurus,

I have a need where in i require to execute below code in Anonymous block,

what changes i need to make in below scheduled apex to make it execute in anonymous block,i just need to execute the code 
 
global class ERT_CaseCopyBatch implements Database.Batchable<sObject>, Database.Stateful {
    private List<ERT_Case_Type__c> processedRecords;
global Database.QueryLocator start(Database.BatchableContext BC) {
    processedRecords = new List<ERT_Case_Type__c>();
    // collect the batches of records or objects to be passed to execute        
    String query = 'SELECT Case__c, Level_1__c, Level_2__c,Level_3__c,UniqueGUID__c  FROM ERT_Case_Type__c WHERE createddate = today AND IsProcessed__c = false';
    return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<ERT_Case_Type__c> exeList) {   
    // process each batch of records
    List<Case_Type__c> listCTD = new List<Case_Type__c>();
   
    for(ERT_Case_Type__c exe : exeList)
    {        
        listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c,ERTUniqueGUID__c=exe.UniqueGUID__c));
       exe.IsProcessed__c = true;    
    }
    try {
        
        insert listCTD;
        //only successful batches will be processed 
        processedRecords.addAll(exeList);
        update processedRecords;
    } catch(Exception e) {
        System.debug(e);
    }
}   
global void finish(Database.BatchableContext BC) {
   
  
}
}

Thanks in advance,
Fiona
Best Answer chosen by fiona gentry
SwethaSwetha (Salesforce Developers) 
HI Fiona,
>To run the batch class, you need to execute below:
Id batchJobId = Database.executeBatch(new ERT_CaseCopyBatch(), 200);
The batchJobId will contain the job Id of the batch. To monitor or stop the execution of the batch Apex job: from Setup, enter Apex Jobs in the Quick Find box, then select Apex Jobs. 200 is the batch size.


>If you want to schedule this batch class you need to use scheduler class as below
global class Scheduler_class implements Schedulable{

    public static String sched = '0 00 00 * * ?';  //this runs Every Day at Midnight.You can change this as per your need

    global static String scheduleMe() {
        Scheduler_class SC = new Scheduler_class(); 
        return System.schedule('My batch Job', sched, SC);
    }

    global void execute(SchedulableContext sc) {

        ERT_CaseCopyBatch  b1 = new ERT_CaseCopyBatch();
        ID batchprocessid = Database.executeBatch(b1,50);           
    }
}

Then you just need to run Scheduler_class.scheduleMe(); from execute anonymous and it will be scheduled to run at your interval. 

Related:https://salesforce.stackexchange.com/questions/24467/scheduling-batch-apex

If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Fiona,
>To run the batch class, you need to execute below:
Id batchJobId = Database.executeBatch(new ERT_CaseCopyBatch(), 200);
The batchJobId will contain the job Id of the batch. To monitor or stop the execution of the batch Apex job: from Setup, enter Apex Jobs in the Quick Find box, then select Apex Jobs. 200 is the batch size.


>If you want to schedule this batch class you need to use scheduler class as below
global class Scheduler_class implements Schedulable{

    public static String sched = '0 00 00 * * ?';  //this runs Every Day at Midnight.You can change this as per your need

    global static String scheduleMe() {
        Scheduler_class SC = new Scheduler_class(); 
        return System.schedule('My batch Job', sched, SC);
    }

    global void execute(SchedulableContext sc) {

        ERT_CaseCopyBatch  b1 = new ERT_CaseCopyBatch();
        ID batchprocessid = Database.executeBatch(b1,50);           
    }
}

Then you just need to run Scheduler_class.scheduleMe(); from execute anonymous and it will be scheduled to run at your interval. 

Related:https://salesforce.stackexchange.com/questions/24467/scheduling-batch-apex

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi fiona gentry,
Kindly find solution.

You can execute this Batch Class from Anonymous  Window in your Org by just putting this code.
Id batchJobId = Database.executeBatch(new ERT_CaseCopyBatch(), 200);
If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.