Jan 14, 2025
352 Views

How to schedule the batch class at specific time ?

Written by

To invoke the Apex batch classes to run at specific times, first we need to implement the Schedulable interface for the Apex class, then specify the schedule using either the standard Salesforce Schedule Apex page in the user interface, or we can use the System.schedule method.

The scheduler class runs as system—all classes are executed, may or may not the user have the permission to execute the class.

To monitor or stop the execution of a scheduled Apex jobs using the Salesforce UI interface, go on Setup, enter the Scheduled Jobs in the Quick Find box, and then select Scheduled Jobs option.

The Schedulable interface that contains one method that must be implemented always, i.e execute.

global void execute(SchedulableContext sc){}

The implemented method should always be declared as global or public.

This method is used to instantiate the class you want to schedule.

Below is the example that implements the Schedulable interface for a class called batchable and it also implements the Schedulable interface for a batch Apex class is called the batchable :-

global class scheduledBatchable implements Schedulable {
    global void execute(SchedulableContext sc) {
        batchable b = new batchable(); database.executebatch(b);
    }
}

The following example is used to implement the System.Schedule method for the above class.

scheduledBatchable batch = new scheduledBatchable();
String sch = '0 15 10 * * ? 2005';
String jobIDNew = system.schedule('Batch Scheduled', sch, batch);

The above expression is called Cron expression. Cron expression is used to schedule the batch class a specific time interval which cannot be scheduled by the Salesforce user interface. The above Cron expression denotes the time as 10:15 am every day during the year 2005. It starts as Seconds & Minutes & Hours & Day of Month, Month & Day of week & Year.

Similarly to schedule the batch class for every 5 minutes we have to write the Cron expression. And schedule the batch class using the Cron expression as the 5 mins schedule is not supported by the salesforce user Interface.

The following example below schedule the Batch Apex class for every 5 minutes:

String CRON_EXPR01 = '0 5 * * * ?' ;
String CRON_EXPR02 = '0 10 * * * ?' ;
String CRON_EXPR03 = '0 15 * * * ?' ;
String CRON_EXPR04 = '0 20 * * * ?' ;
String CRON_EXPR05 = '0 25 * * * ?' ;
String CRON_EXPR06 = '0 30 * * * ?' ;
String CRON_EXPR07 = '0 35 * * * ?' ;
String CRON_EXPR08 = '0 40 * * * ?' ;
String CRON_EXPR09 = '0 45 * * * ?' ;
String CRON_EXPR10 = '0 50 * * * ?' ;
String CRON_EXPR11 = '0 55 * * * ?' ;
SchedulerUpdateAccount batch1 = new SchedulerUpdateAccount();
SchedulerUpdateAccount batch2 = new SchedulerUpdateAccount();
SchedulerUpdateAccount batch3 = new SchedulerUpdateAccount();
SchedulerUpdateAccount batch4 = new SchedulerUpdateAccount();
SchedulerUpdateAccount batch5 = new SchedulerUpdateAccount();
SchedulerUpdateAccount batch6 = new SchedulerUpdateAccount();
SchedulerUpdateAccount batch7 = new SchedulerUpdateAccount();
SchedulerUpdateAccount batch8 = new SchedulerUpdateAccount();
SchedulerUpdateAccount batch9 = new SchedulerUpdateAccount();
SchedulerUpdateAccount batch10 = new SchedulerUpdateAccount();
SchedulerUpdateAccount batch11 = new SchedulerUpdateAccount();
System.schedule('Hourly Account Batch Schedule job1', CRON_EXPR01, batch1);
System.schedule('Hourly Account Batch Schedule job2', CRON_EXPR02, batch2);
System.schedule('Hourly Account Batch Schedule job3', CRON_EXPR03, batch3);
System.schedule('Hourly Account Batch Schedule job4', CRON_EXPR04, batch4);
System.schedule('Hourly Account Batch Schedule job5', CRON_EXPR05, batch5);
System.schedule('Hourly Account Batch Schedule job6', CRON_EXPR06, batch6);
System.schedule('Hourly Account Batch Schedule job7', CRON_EXPR07, batch7);
System.schedule('Hourly Account Batch Schedule job8', CRON_EXPR08, batch8);
System.schedule('Hourly Account Batch Schedule job9', CRON_EXPR09, batch9);
System.schedule('Hourly Account Batch Schedule job10', CRON_EXPR10, batch10);
System.schedule('Hourly Account Batch Schedule job11', CRON_EXPR11, batch11);

 

Article Categories:
Salesforce
https://thestarbiznews.com

Ramya Singh isn't your average tech blogger. Sure, she's got the brains to understand the latest algorithms and the jargon to explain them in plain English. But she's also got a twinkle in her eye and a way of weaving technology into the fabric of everyday life that makes it nothing short of fascinating. Whether she's reviewing the latest smartphone, exploring the potential of virtual reality, or delving into the ethical implications of artificial intelligence, Ramya does it with a contagious enthusiasm that makes you want to learn more, do more, and be a part of the exciting world of tech. So, if you're looking for a tech blog that's informative, inspiring, and just plain fun, follow Ramya Singh. She'll take you on a journey through the ever-evolving landscape of technology, and you might just find yourself a little bit more tech-savvy (and a lot more excited) along the way.