Apex Scheduler in salesforce

Apex Scheduler in salesforce

If you want to schedule apex class to run at regular intervals then you need to write an Apex class that implement Schedulable interface.

Schedulable interface contains one method which need to be implemented.

global void execute(SchedulableContext sc)


/* Logic goes here */


Implemented method should be declared either as global or public.

using the below code, you can able to schedule this batch Apex class

global class ScheduleApex implements Schedulable 
 global void execute(SchedulableContext SC) 
 contactBatch cb = new contactBatch(); 


Run this cron expression code in developer console to schedule the batch Apex job to run for every one hour,

System.schedule('Hourly Job ', '0 0 * * * ?', new ScheduleApex() );

You can do more customization on the above cron expression to run job at specific day, date and time or for every certain minutes or hours. Even scheduling a job can also be done at UI level

Tracking the Progress of a Scheduled Job :-

Once job scheduled you can able to track the progress from cronTrigger table

CronTrigger ct = [SELECT TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :jobID];

How to get job ID to monitor the progress of a scheduled job ?

  •  using System.schedule method, we can able to get job ID

String JobId = System.schedule('Hourly Job ', '0 0 * * * ?', new ScheduleApex() );

Salesforce development guide on schedule Apex :- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

Be the first to comment

Leave a Reply

Your email address will not be published.