Salesforce Developer Interview Questions – Asynchronous Apex – Scheduled Apex
Scheduled Apex
Table of Contents
134. What is Scheduled Apex?
Answer
Scheduled apex is all about running or invoking apex class which will run at a specific time. To schedule an apex class, we need to implement a schedulable interface.
135. When should we use scheduled apex?
Answer
Here are few example of scheduled apex use case
- To update the records of an object periodically . Example update opportunity status to close-lost if it is not updated in the last 90 days.
- To sync the external system every night – Example run a scheduled job every night to sync the data from salesforce to external system every night
- One off update to the record after data migration . Example after data migration from other system we can run a schedule job one off to updated the records based on some logic
- And many more….
136. Which interface should be implemented for schedulable class?
Answer
To schedule an apex class, it should implement salesforce provided interface “Schedulable”
137. Is there any mandatory method(s) that must be implemented?
Answer
execute () methods must be implemented for scheduled apex. And the execute method must be declared as global or public.
![](https://www.ashfaqmohammed.com/wp-content/uploads/2023/08/137-Is-there-any-mandatory-methods-that-must-be-implemented.png)
138. What is schedulableContext?
Answer
It is a parameter for the execute method of a class that implements a schedulable interface. SchedulableContext contains the scheduled job ID which can be retrieved using the getTriggerID method.
We can use getTriggerId to track the progress of a scheduled job through a query on CronTrigger.
We can also monitor it from Setup → Scheduled job
139. What is CronTrigger?
Answer
CronTrigger is an object . When we schedule an apex class, a cronTrigger record is created that represents the scheduled job. We can query CronTrigger objects to obtain more information about the job like the number of times the job has run, and date and time when the job is scheduled to run again.
![](https://www.ashfaqmohammed.com/wp-content/uploads/2023/08/139-What-is-CronTrigger.png)
140. How can we schedule the apex class?
Answer
There are two ways to schedule an apex class.
Using developer console : Write the following code in an anonymous window and run it to schedule it.
![](https://www.ashfaqmohammed.com/wp-content/uploads/2023/08/140-How-can-we-schedule-the-apex-class-1-1024x175.png)
Here:
10 represents seconds
30 represents minutes
8 represents hour of the day
15 represents 15th day of month
6 represents month of the year
? Specifies no specific value. This option is only available for Day_of_month and Day_of_week. It’s typically used when specifying a value for one and not the other.
From the UI
From Setup , search apex in quick find box then Select apex classes
Go to Setup → apex classes
We will see a “Schedule apex” button. We can set up the timing from there
![](https://www.ashfaqmohammed.com/wp-content/uploads/2023/08/140-How-can-we-schedule-the-apex-class-2-1024x434.png)
141. What is CORN_EXP?
Answer
CORN_EXP is called as Cron Expression and it is used to define the scheduling time it has 7 parameters including one optional year parameter
An expression is written in the form of “Seconds, Minutes, Hours, Day_of_month, Day_of_week, optional_year.
142. Can you explain or elaborate all the special characters used in CORN_EXP?
Answer
Special character | Description |
, | Delimits values. For example, use JAN, MAR, APR to specify more than one month. |
– | Specifies a range. For example, use JAN-MAR to specify more than one month. |
* | Specifies all values. For example, if Month is specified as *, the job is scheduled for every month. |
? | Specifies no specific value. This option is only available for Day_of_month and Day_of_week. It’s typically used when specifying a value for one and not the other. |
/ | Specifies increments. The number before the slash specifies when the intervals will begin, and the number after the slash is the interval amount. For example, if you specify 1/5 for Day_of_month, the Apex class runs every fifth day of the month, starting on the first of the month. |
L | Specifies the end of a range (last). This option is only available for Day_of_month and Day_of_week. When used with Day of month, L always means the last day of the month, such as January 31, February 29 (for leap years), and so on. When used with Day_of_week by itself, it always means 7 or SAT. When used with a Day_of_week value, it means the last of that type of day in the month. For example, if you specify 2L, you’re specifying the last Monday of the month. Don’t use a range of values with L as the results can be unexpected. |
W | Specifies the nearest weekday (Monday-Friday) of the given day. This option is only available for Day_of_month. For example, if you specify 20W, and the 20th is a Saturday, the class runs on the 19th. If you specify 1W, and the first is a Saturday, the class doesn’t run in the previous month, but on the third, which is the following Monday. |
# | Specifies the nth day of the month, in the format weekday#day_of_month. This option is only available for Day_of_week. The number before the # specifies weekday (SUN-SAT). The number after the # specifies the day of the month. For example, specifying 2#1 means the class runs on the first Monday of every month. |
143. Can you explain or elaborate all the values used in CORN_EXP?
Answer
Name | Values | Special Characters |
Seconds | 0–59 | None |
Minutes | 0–59 | None |
Hours | 0–23 | , – * / |
Day_of_month | 1–31 | , – * ? / L W |
Month | 1–12 or the following: JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC | , – * / |
Day_of_week | 1–7 or the following: SUN MON TUE WED THU FRI SAT | , – * ? / L # |
optional_year | null or 1970–2099 | , – * / |