Salesforce Interview QuestionsSalesforce

Salesforce Apex Scheduler Interview Questions and Answers

1. What is Apex Scheduler?

Apex Scheduler is an ability to invoke an Apex class to run at specific times.

2. What could be the use cases for this?

There could be many use cases
For example:

  • Sending Periodic notifications to users.
  • NewsLetters on the 15th of every month.
  • Tasks related to real-time updates.

3. How can you schedule an Apex class using Apex Scheduler?

To schedule an Apex class using Apex Scheduler, you need to implement the Schedulable interface and define the execute() method. Then, you can use the System.schedule() method to specify the schedule for the class.

4. Can you write the basic Apex structure of a class that can be scheduled?

public class SampleSchedulerClass implements Schedulable {
 public void execute(SchedulableContext SC) {
    //Some code that would be called

5. What is SchedulableContext?

It contains the scheduled job ID using the getTriggerID() method. This interface is implemented internally by Apex.

6. what would be the use of that Job ID?

Using ID, we can track the Progress of a Scheduled Job through a query on CronTrigger. The ID is of type CronTrigger.
Moreover, we can go to Setup-> Scheduled Jobs too, for monitoring.

7. How can I schedule the apex class?

Once you are done with the implementation, you can schedule it using either of the following methods:

Through the Developer Console using the System.schedule method.

SampleSchedulerClass obj = new SampleSchedulerClass();
String CRON_EXP = ’20 30 8 10 2 ?’;
String jobID = system.schedule(‘Sample Scheduler Job’, CRON_EXP, obj);


Via the UI: Navigate to Setup -> Develop -> Apex Classes. You will find a “Schedule Apex” button there.

schedule apex salesforce

8. As you mentioned that the jobID can be used to query the progress, how can we track it?

We can simply fire a query to get information about current job

CronTrigger ct = [SELECT CronExpression,CronJobDetailId,PreviousFireTime,TimesTriggered, NextFireTime,State,StartTime,EndTime FROM CronTrigger WHERE Id =: jobID];

9. What is this CronJobDetailId?

CronJobDetail is the parent CronTrigger that saves a job’s information like Name, JobType, etc.
JobType value is always 7 for Scheduled Apex.
we can get these details too using the same CronTrigger query.

CronTrigger job = [SELECT Id, CronJobDetail.Id, CronJobDetail.Name, CronJobDetail.JobType FROM CronTrigger where Id =: jobId];


Can query on CronJobDetail with CronJobDetailId.

CronJobDetail ctd = [SELECT Id, Name, JobType 
 FROM CronJobDetail WHERE Id = :CronJobDetailId];

10. what is CRON_EXP?

It is called CRON EXPRESSION which is used to define the scheduling time. it has 6 to 7 inputs,

Seconds, Minutes, Hours, Day_of_month, Month, Day_of_week, optional_year

it uses some special characters too

? — No Value
* — All Values
? — Specifies no specific value
/-Specifies increments. The number before the slash specifies when the intervals will begin, and the number after the slash is the interval amount.
#-Specifies the nth day of the month, in the format weekday#day_of_month
L-Specifies the end of a range (last)
W-Specifies the nearest weekday(Monday-Friday) of the given day

11. what is the purpose of special characters?

The purpose of special characters is to define a range or interval. They are like wildcard characters in database queries.

For example, If I want to run a job at 12 midnight on the 1st of every month in 2021,

my CRON_EXP would be

“0 0 0 1 * 2021”

where * = all values possible for a month i.e. 1–12

12. Let’s say, I have scheduled an apex class to run at 3 pm today, and at 3 pm, some batches are already running. How will your apex class code run at 3 pm?

It will get scheduled on time because Salesforce schedules the class for execution at the specified time, But the Actual execution may be delayed based on service availability. So, it will run once everything is done.

14. Let’s say, I have scheduled an apex class to run at 3 pm today, and at 3:15 pm, downtime occurred. What will happen now?

There could be 2 cases,

  1. Before starting the job, downtime occurred, in this case, Apex jobs will be scheduled to run after the service comes back up, when system resources become available.
  2. If a scheduled Apex job was running when downtime occurred, the job is rolled back and scheduled again after the service comes back up.

15. What async processes, I can schedule?

We can schedule

  • Batch Apex (can call a batch from Scheduled Apex)
  • Future Method (can call future from Scheduled Apex)
  • queueable apex (can call queueable form Scheduled Apex)

16. Which of the above can cause an issue?

Calling the Future methods from scheduled apex can cause some issue:

  • the future won’t guarantee it’s execution on time, so tracking is not possible. Also, we lose transaction control because the scheduled apex gets over as soon as it fires the future method call.
  • there is a limit to the number of future calls based on your number of licenses

17. Maximum, How many jobs can be scheduled at any point in time?

You can only have 100 scheduled Apex jobs at one time.

Either we can verify the count by viewing the Scheduled Jobs page OR

we can simply query with CronJobDetail.JobType as 7 (scheduled Apex) on CronTrigger

SELECT COUNT() FROM CronTrigger WHERE CronJobDetail.JobType = ‘7’

18. How can you stop the execution of a job that has already been scheduled?

To stop the execution of a job that has been scheduled, use the System.abortJob method with the ID returned by the getTriggerID method.

19. Can we write a Unit test case for a scheduled Apex?

Yes we can, Use the Test methods startTest and stopTest around the System.schedule method to ensure it finishes before continuing your test.
All asynchronous calls made after the startTest method are collected by the system.
When stopTest is executed, all asynchronous processes are run synchronously.

20. Can you tell Best Practices to be followed while using Scheduled Apex?

  • Synchronous Web service callouts are not supported from scheduled Apex. To be able to make callouts, make an asynchronous callout.
  • While scheduling a job from the trigger, we must be able to guarantee that the trigger won’t add more scheduled classes than the limit.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button