Salesforce Developer Interview Questions – Asynchronous Apex – Scheduled Apex
Table of Contents
134. What is Scheduled Apex?
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?
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?
To schedule an apex class, it should implement salesforce provided interface “Schedulable”
137. Is there any mandatory method(s) that must be implemented?
execute () methods must be implemented for scheduled apex. And the execute method must be declared as global or public.
138. What is schedulableContext?
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?
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.
140. How can we schedule the apex class?
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.
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
141. What is CORN_EXP?
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?
|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.
|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.
|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?
|, – * /
|, – * ? / L W
|, – * /
|, – * ? / L #
|null or 1970–2099
|, – * /