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-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-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-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.
Trailblazer Profile | LinkedIn | Salesforce Blog | Facebook | Youtube Channel | WhatsApp Community
Comments
Post a Comment