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
Shiva RajendranShiva Rajendran 

Using schedular to mimic batch class functionality

I have a batch class which simply reads all the records and update a checkbox and add *10 to a integer field value.
Now i have mimiced the same functionality using a schedular , i want to understand the disadvange or the effectiveness of this approach in salesforce.

Schedular Class which schedules the schedular that act as batch class

Section 1 (Batch Class mimic Schedular implementation): 
global class SchedularToScheduleSchedular_sch implements Schedulable {
    global void execute(SchedulableContext ctx) {
	
		SchedularMimicBatch schdu = new SchedularMimicBatch(); 
		schdu.execute(null);
    }
}

Batch Mimicing Schedular :
 
global class SchedularMimicBatch implements Schedulable{
	global final Integer recordSize;
    
    global SchedularMimicBatch(integer recordSize)
    {
        this.recordSize=recordSize;
        
    }
    global SchedularMimicBatch()
    {
        this.recordSize=200;
        
    }
    global void execute(SchedulableContext ctx) {
        try{
        List<shivaKalinga__BatchScheduleEg__c> currentRecordsToProcess=[select id,shivaKalinga__isValidated__c,shivaKalinga__intData__c from shivaKalinga__BatchScheduleEg__c where shivaKalinga__isValidated__c=false limit :recordSize];
        if(currentRecordsToProcess.size()!=0)
        {
           
            for(shivaKalinga__BatchScheduleEg__c ind:currentRecordsToProcess)
        {
            ind.shivaKalinga__isValidated__c=true;
            ind.shivaKalinga__intData__c=(ind.shivaKalinga__intData__c*10);
        }
            update currentRecordsToProcess;
            for (CronTrigger ct : [SELECT Id FROM CronTrigger]) {
                System.debug(ct.Id);
                   				 System.abortJob(ct.Id);

            }
            SchedularMimicBatch schdu = new SchedularMimicBatch(200); 
			schdu.execute(null);
        }
        
        else
        {
             for (CronTrigger ct : [SELECT Id FROM CronTrigger]) {
                System.debug(ct.Id);
   				 System.abortJob(ct.Id);
			}
        }
        }
        catch(Exception e)
        {
            System.debug('error found');
        }
    }
}
In execute anonymous execute this to execute the schedular :
SchedularToScheduleSchedular_sch abc = new SchedularToScheduleSchedular_sch(); 
abc.execute(null);



Section 2  (Actual Batch Implementation) :

Schedular to Batch :
global class SchedularToScheduleBatch_sch implements Schedulable {
  public static String CRON_EXP = '0 0 0 3 9 ? 2022';
    global void execute(SchedulableContext ctx) {
		/*CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
			FROM CronTrigger WHERE Id = :ctx.getTriggerId()];
		System.assertEquals(CRON_EXP, ct.CronExpression);
        System.debug(CRON_EXP);
        System.debug(ct);

    	System.debug(ctx );*/
		Database.executeBatch(new BatchClass_batch(),200); 
	}
}

Batch Class :
 
global class BatchClass_batch implements Database.Batchable<sObject>{
global final String query;
    global BatchClass_batch()
    {
        query='select id,shivaKalinga__isValidated__c,shivaKalinga__intData__c from shivaKalinga__BatchScheduleEg__c';
    }
global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
     for(sobject s : scope){
         Integer ii=Integer.valueOf(s.get('shivaKalinga__intData__c'));
         	ii*=10;
			s.put('shivaKalinga__intData__c',ii);
         	s.put('shivaKalinga__intData__c',true);
     }
     update scope;
    }

   global void finish(Database.BatchableContext BC){
   }
}
In execute anonymous execute this to execute the schedular :
 
SchedularToScheduleBatch_schabc = new SchedularToScheduleBatch_sch(); 
abc.execute(null);

Both Section 1 and Section 2 give's the same result and will work for huge data. 
Is there any disadvantages in using the second approach instead of batch class? Since if it doesn't,  it will help in running more batch class execution concurrently or can be used to run 5 batch class and one batch mimic at the same time.

Thanks and Regards,
Shiva RV





SchedularToScheduleBatch_sch