Salesforce Asynchronous Apex: How to use Cron Expressions to enqueue the Schedulable Job

 

Looking for Salesforce Training & HandsOn Projects?

Trailblazer Profile | LinkedIn | Salesforce Blog | Facebook | Youtube Channel | WhatsApp Community

A Cron expression is a string of characters that specifies a schedule for running a command.

Cron expressions are a powerful tool for automating tasks. They can be used to run commands at specific times, on specific days, or on specific intervals.

Cron expressions are used by many different applications. Salesforce is using Cron expression to allow scheduling jobs at runtime.

We can read more about Cron expression here Cron Expressions

You can refer to my earlier posts on Schedulable Job to get a context around this concept:

In this article, we will explore how to schedule the “Schedulable” Job using CRON expression 

We can start with a simple demo to explain the concept in the following steps:

Step-1: First we will explore the Opportunity records & make sure no records are created already using Cron expression. This will be helpful later to validate newly created records




Step-2: Add a public Apex class “ScheduleClassWithCronExp” that is implementing the “Schedulable” interface


Step-3:  Implement the “execute” function & add a logic to create an Opportunity record. 


Step-4: Provide a valid Account Id as a parent to this Opportunity 


Step-5:
 Write the success statement to the debug log. Write a log is always a good idea to validate the job execution at a later stage



Step-6: Launch “Execute Anonymous Window” from Debug Menu



Step-7: Specify the Cron expression as per business needs, in this demo we specify the Cron expression to set a schedule for 1:45 pm every day. 


Step-8: We would use the “System.schedule” method to schedule the job


Step-9:
  “System.schedule” contains 3 parameters “Job Name”, “Cron Expression” & “Apex Class Instance that we are planning to schedule as a Job”


Step-10: We can find the list of Scheduled Jobs by going to Setup > Search Job


Step-11: Click on “Scheduled Jobs”


Step-12: We can see the Job Name that we just scheduled in the steps earlier


Step-13: We can also check the upcoming execute window for the Job to run


Step-14:
 We can also check the type of this Job is “Scheduled Apex” which makes perfect sense since we schedule the Apex class to run as a Job


Step-15: Once the job gets kicked as per the execution window, we can see the “Started” Column updated with the Start Date & Time for the job


Step-16: Once the execution window arrives, the Salesforce platform kicks in the Scheduled Job which internally executes the “execute” method & creates the Opportunity Record



Step-17: We can review the output of the debug statement after the Job gets executed 


There is more to this story, we will be having more articles regarding Other Design Patterns that can be used to manage the job efficiently. So stay tuned.


Hope you enjoyed this article. Please leave your comments to let me know how you like the content and how you find it helpful to learn the topic. 


Trailblazer Profile | LinkedIn | Salesforce Blog | Facebook | Youtube Channel | WhatsApp Community


















Comments