Monday, October 3, 2016

Asynchronous Apex- Apex Scheduler, Batch Apex & Future Methods Implementation Tricks

Salesforce provide different options to run asynchronous jobs which can run in background whenever salesforce resources will be available.

Apex Scheduler is one of the options through which you can schedule a job to run after certain interval of time or to run at particular time everyday, every week, every month etc.

Batch Apex allows you to process large volume of data. Normal apex class will hit governor limits if we process large volume of data. Batch Apex split the records in different batched and you will get new governor limits for each batch.

Future methods runs asynchronously which can be used to perform some operation in background. When you are executing particular code and you want to perform some other operation and don't want to wait for it to get completed, then you can use future method to perform that operation and you can continue executing your code. You can consider that future methods starts new thread or execution.

Now we are going to cover different scenarios related to future methods and apex schedulers and batch apex.

  • Is it possible to call future method from apex scheduler or not?

Below is sample code which I tested for this scenario. After scheduling the scheduler, I checked the debug logs and it was displaying logs for future handler  and debug statement present in future method was present in logs.

//Scheduled Apex 
public class DemoScheduler1 implements Schedulable{
    public void execute(SchedulableContext sc){
        system.debug('*******Going to call future method ');
//apex class containing future method
public class DemoAsynchronousTest{
    public static void futureMethodCallFromScheduler(){
        system.debug('******futureMethodCallFromScheduler get called');

  • Is it possible to do Synchronous Web service callouts from scheduled apex?

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.

  • Can we call scheduler from future method?

  • Can we call future method from batch apex (from execute method)?

  • Is there is any way through which we can call future method from batch apex?
As we know that a webservice can be called from batch class and webservice can call @future method. So in your batch class call webservice and which can call your @future method.         
Also you can call future method from finish method in batch class.

  • What all different things which we need to consider while using future methods?
  1. Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
  2. You cannot call a method annotated with future from a method that also has the future annotation. Nor can you call a trigger from an annotated method that calls another annotated method.
  3. Future methods cannot be called from batch class.

  • Can we modify the scheduler class or classes referenced by this scheduler class if any scheduled job is pending?

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.

  • What we can do if we have to deploy the scheduler class or classes referenced by this scheduler class if any scheduled job is pending for that scheduler class in target org?
By default, changes to Apex code that have Apex jobs pending or in progress can’t be deployed. To deploy these changes, do one of the following.
  • Cancel Apex jobs before deploying changes to Apex code. Reschedule the jobs after the              deployment.
  • Enable deployments with Apex jobs in the Salesforce user interface in the Deployment Settings page.



  1. I acknowledge the author for his brilliant work for making this exceptionally useful and informative content to guide us.

    Salesforce Implementation Services

  2. This is something that you just read and read. You just can’t get tired of it.

  3. This comment has been removed by the author.

  4. We cant call future method from batch class we need to use queueable for this.

  5. Really we can call future method from execute method of batch class?

  6. for calling a future method from batch finish method it giving this error
    System.LimitException: Too many future calls: 1