Q. Explain code to schedule batch Apex to run at regular intervals?
global class SampleBatchScheduler implements Schedulable { // Execute at regular intervals global void execute(SchedulableContext ctx){ String soql = 'SELECT Id, Name FROM Account'; SampleBatch batch = new SampleBatch(soql); Database.executebatch(batch, 200); } }
Once you are implemented schedulable interface use system.schedulable method to execute the class.
system.schedule() method takes 3 parameters :
1. Name of the job
2. An expression that is used to represent the time and date of the operation.
3. The object of the class which you want to execute.
An expression is written in the form of ‘Seconds, minutes, hours, day of the month, month day of the week, optional year.’
‘Seconds’ : 0-60
‘Min’ : 0-60
‘Hours’ : 0-24
‘Day-Months’ : 1-31
‘Month’ : 1-12
‘Day-Week’ : 1-7
‘Optional Year’ : —
Q. What is return type of system.schedule?
System.schedule method returns the job ID in string format.
String jobID = system.schedule('Merge Job', sch, m);
class MySchedule implements Schedulable { public void execute(SchedulableContext BC) { //---------// } MySchedule mysc = New MySchedule(); String str = '0 0 10 * 3 2'; // MyJob is Name, str is Time Format, mysc is Object System.schedule('MyJob', str, mysc); }
‘0 30 12 ? 1 3’
Q. Want to schedule batch job at one time only and not again. How to do it?
System.scheduleBatch() is used to run a schedule a batch job only once for a future time. This method has got 3 parameters.
param 1 : Instance of a class that implements Database.Batchable interface.
param 2 : Job name.
param 3 : Time interval after which the job should start executing.
param 4 : It’s an optional parameter which will define the no. of that processed at a time. The system.scheduleBatch() returns the scheduled job Id.
We can use the job Id to abort the job.
This method returns the scheduled job ID also called CronTrigger ID.
String cronID = System.scheduleBatch(reassign, 'job example', 1); CronTrigger ct = [SELECT Id, TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :cronID];
This method is available only for batch classes and doesn’t require the implementation of the Schedulable interface. This makes it easy to schedule a batch job for one execution.
Q. How to get count of Apex Scheduled Job programmatically?
You can programmatically query the CronTrigger and CronJobDetail objects to get the count of Apex scheduled jobs.
Q. If there are one or more active scheduled jobs for an Apex class, can you update the class or any classes referenced in Salesforce UI?
If there are one or more active scheduled jobs for an Apex class, you cannot update the class or any classes referenced by this class through the Salesforce user interface. However, you can enable deployments to update the class with active scheduled jobs by using the Metadata API
Q. Does Apex Scheduler runs in system mode?
The scheduler runs as system—all classes are executed, whether or not the user has permission to execute the class.
Q. How to Call batch apex from schedulable class?
Create instance of batchClass and then pass the instance in database.executebatch
batchable b = new batchable(); database.executebatch(b);
An easier way to schedule a batch job is to call the System.scheduleBatch method without having to implement the Schedulable interface.
Q. How to get Job name and job type for Scheduled jobs?
You can get job’s name and the job’s type from the CronJobDetail record associated with the CronTrigger record.
Q. Callout is not supported in Scheduled Apex so what is the alternative?
Synchronous Web service callouts are not supported from scheduled Apex. To be able to make callouts, make an asynchronous callout by placing the callout in a method annotated with @future(callout=true) and call this method from scheduled Apex. However, if your scheduled Apex executes a batch job, callouts are supported from the batch class.
Q. What are limitations of Scheduled Apex?
1. We can schedule only 100 jobs at a time.
2. Max no. of apex schedule jobs in 24 hours is 2,50,000 number of jobs (can change with salesforce updates).
3. Synchronous WebService callouts are not supported in schedulable Apex.