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