SalesforceBlue

Feel the rhythm of Salesforce

Apex

Scheduled Apex Simplified

Scheduled Apex allows us to schedule a class to be executed on a specific day and time either on a recurring or one-time basis.

This becomes handy when you have some data processing jobs which are required to be run daily at a specific time in a day. For Example, You can write a scheduled class to pull currency exchange data from an external system to Salesforce daily at 6:00 AM.

Code Block Of Scheduled Apex:

We can create Scheduled Apex Class by implementing the Schdedulable interface and defining the execute method.

public class ScheduledApex implements Schedulable {
    
    public void execute(SchedulableContext ctx) {
        
        // Awesome Code Here!
    }   
}
Invoking Scheduled Apex:

We can invoke a scheduled apex by scheduling it from either way:

Scheduling From UI:

Go to Apex Class > Click On “Scheduled Apex” Button > Enter the Job Name > Select the Schedulable implemented class and give the Date & Time Details. Below is an image of the Scheduled Apex screen from UI.

Scheduled Apex

Scheduling From Apex:

We can schedule from Apex using System.schedule method. It accepts three parameters: job name, cron expressions, and an instance of Schedulable implemented class.

We have covered cron expressions below.

System.schedule('My-Scheduled-Job', '0 0 13 * * ?', new ScheduledApex());

System.schedule method returns a CronTrigger Id, which can obtain more information on the scheduled job.

Id cronTriggerId = System.schedule('My-Scheduled-Job-3', '0 0 13 * * ?', new ScheduledApex());
CronTrigger ct = [Select TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :cronTriggerId];
Apex Cron Expressions:

Cron expressions represent the scheduled date & time for your job.

It has the following syntax:

seconds Minutes Hours Day_of_month Month Day_of_week optional_year

You have to provide a minimum of six arguments for cron expression.

The below Table Specifies the value that can be given to each of the entities present in cron expressions.

NameValueSpecial Characters
Seconds0-59none
Minutes0-59none
Hours0-23, – * /
Day of the Month1-31, – * / ? L W
Month1-12 or JAN-DEC, – * /
Day of Week1-7 or SUN-SAT, – * / ? L #
Optional Yearnull or 1970-2099, – * /

The below table specifies the significance of special characters in cron expressions.

Special CharactersDescription
,Delimits values.
E.g. Jan, Feb, Mar to specify more than one value.
Specify a range.
E.g. JAN-MAR
*Specify all values.
E.g. if the month is *, the job is scheduled for every month.
?Specifies no specific values.
Available only for Day_of_month, Day_of_Week.
Generally used when specifying a value for one and not the other.
/Specify increment.
1/5 for Day of the month makes apex class runs every 5th day of the month starting from 1st of the month.
LSpecify last.
Available for Day_of_month/ Day_of_Week.
When used with the month it means the last day of the month.
For Day of the week, it means SAT. When used with day_of_week value says 2L it means the last Monday of the month.
Do not use a range of values with L.
WSpecify the nearest weekday.
Only available for Day_of_month.
For example, if you specify 20W, and the 20th is a Saturday, the class runs on the 19th.
Use the L and W together to specify the last weekday of the month.
#Specify the nth day of the month in the format weekday#day_of_month.
Only available for Day_of_Week.
E.g. 2#3 means apex class runs on the third Monday of every month.

Let’s see some examples of Cron expressions:

Cron ExpressionDescription
0 0 13 * * ?The class runs every day at 1 pm.
0 0 22 ? * 6LThe class runs on the last Friday of every month at 10 pm.
0 0 10 ? * MON-FRIThe class runs Monday to Friday of every month at 10 am.
0 0 20 * * ? 2010Class runs every day at 8 PM during the year 2010.

Callouts from Scheduled Apex:

We can make synchronous callouts to external services from the scheduled APEX. To do so place the callout in a method annotated with @future(callout=true) and call this method from scheduled Apex.

if your scheduler executes a batch job, callouts are supported from the batch class.

Monitoring Scheduled Apex:

We can monitor the status of all jobs in the Salesforce user interface by going to Setup > Scheduled Jobs > | Apex Jobs.

Below images show the screen for monitoring Scheduled Jobs:

Scheduled Apex Governor Limits Considerations:

We can have a maximum of 100 scheduled apex jobs at one time. If you face an issue scheduling it then you may have to change the time and then reschedule.

We have to be a bit careful when we are planning to schedule a class from the trigger. We must ensure that the trigger should not add more scheduled jobs than the limit.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


One thought on “Scheduled Apex Simplified

Leave a Reply

Your email address will not be published. Required fields are marked *