Salesforce Developer Interview Questions – Asynchronous Apex – Scheduled Apex Continue
Scheduled Apex Continue
Table of Contents
144. Can you write some examples of how to use the expressions or CORN_EXP?
|0 0 13 * * ?||Class runs every day at 1 PM|
|0 0 22 ? * 6L||Class runs the last Friday of every month at 10 PM.|
|0 0 10 ? * MON-FRI||Class runs Monday through Friday at 10 AM.|
|0 0 20 * * ? 2010||Class runs every day at 8 PM during the year 2010.|
145. Which all types of classes or asynchronous processes we can schedule?
We can schedule all of them like
- Batch Apex – We can call a batch class from scheduled apex
- Future method – We can call future method from scheduled apex
- Queueable apex – we can call a queueable apex from scheduled apex.
146. Among Batch apex, Future method and Queueable apex , which one can cause issues if we schedule them?
If we call future methods from scheduled apex then it can cause some issues.
Because the future method does not guarantee its execution on time. So, tracking is not possible as we lose transaction control as scheduled apex gets over as soon as it fires the future method.
There is a limit to the number of future calls based on the number of licenses of our org.
147. Can you update the class or any classes referenced in a scheduled job?
If there are one or more active scheduled jobs for an apex class, we cannot update the class or any classes referenced by the apex class though the salesforce user interface (like updating the apex class within the salesforce UI).
However, you can update the class in the sandbox and deploy the class in production with active scheduled job by using metadata API
148. What is the maximum number of jobs that can be scheduled at a given time?
We can only have 100 scheduled apex jobs at a time.
149. How to count the number of scheduled jobs ?
SELECT COUNT() FROM CronTrigger where CronJobDetails.JobType = ‘7’
Here CronJobDetails.JobType = ‘7’ represents scheduled apex
150. Does scheduled apex support Callout?
No, Scheduled apex does not support synchronous web service callouts.
151. If callout is not supported in scheduled apex then what is the alternate way to callout from scheduled apex?
There are two ways to callout from scheduled apex. We should make asynchronous callouts
- Using future method : user future method with callout like @future(callout=true) and call this method from scheduled apex.
- Create a batch apex with callouts (callouts are supported in batch apex) and create a scheduled apex which executes the batch job.
152. Can you tell me about some best practices for scheduled apex?
- If we are planning to schedule a class from a trigger. We must be able to guarantee that the triggers won’t add more scheduled classes than the limit.
- Though it’s possible to do additional processing in the execute method, we recommend that all processing take place in a separate class.
- Synchronous web service callouts are not supported from scheduled apex, to make asynchronous callouts use @feature or Batch apex.