Salesforce Asynchronous Apex: How to work with Schedulable Apex

Looking for Salesforce Training & HandsOn Projects?

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

A Schedulable class in Salesforce is an Apex class that can be scheduled to run at specific times.  

To create a schedulable class, we need to implement the “Schedulable” interface.  

Schedulable Apex is the “public” class that implements the “Schedulable” Interface. This interface contains a method “execute” which the consumer class needs to override to provide the concrete implementation. 

Once you have scheduled a class, it will be executed at the specified time. If the class is already running, it will not be executed again until the next scheduled time. 

Here are some additional things to keep in mind about schedulable classes:

  • Schedulable classes can only be scheduled to run in system mode. This means that the class will be executed even if the user who created the class does not have permission to execute it.
  • Schedulable classes can only be scheduled to run within the current org. They cannot be scheduled to run in other orgs.
  • Schedulable classes can only be scheduled to run for a maximum of 100 days. 

In this article, we will explore how to develop a schedulable class & create some Opportunities using it. 

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. This will be helpful later to validate newly created records


Step-3: Open the “Developer Console” to add a brand new Apex class


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

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

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

Step-7: 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-8: This is an early stage, just to show how the output of the debug statement would look like after the Job gets executed


Step-9: To run this Apex class as a Job we need to first schedule it. To do that go to Setup Search > Search for Apex Classes

Step-10: Click on “Apex Classes”

Step-11: We can see the Apex class is listed & we can schedule it to run as a Job

Step-12: Click on the “Schedule Apex” button


Step-13: Enter a suitable name for the Job

Step-14: Select the required Apex Class using the lookup icon in Apex Class Field.

This lookup will only show those classes that have a “Schedulable” Interface implemented


Step-15:  Select the required schedule for this Job to execute. This will act as the execution window for the Job.

Select Frequency, Start Date, End Date & Start Time. Click on the Save button


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

Step-17: Click on “Scheduled Jobs”

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

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

Step-20: 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-21: Once the execution window arrives, the Salesforce platform kicks in the Scheduled Job which internally executes the “execute” method & creates the Opportunity Record.

We can validate this execution by going back to Opportunity records & make sure new records have been added to the list.


There is more to this story, we will be having more articles regarding Cron Expressions, 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.



















Comments