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.
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 ');
DemoAsynchronousTest.futureMethodCallFromScheduler();
}
}
public class DemoScheduler1 implements Schedulable{
public void execute(SchedulableContext sc){
system.debug('*******Going to call future method ');
DemoAsynchronousTest.futureMethodCallFromScheduler();
}
}
//apex class containing future method
public class DemoAsynchronousTest{
public class DemoAsynchronousTest{
@future
public static void futureMethodCallFromScheduler(){
system.debug('******futureMethodCallFromScheduler get called');
}
}
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?
Yes
- Can we call future method from batch apex (from execute method)?
No
- 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?
- Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
- 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.
- 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.
More Blogs>>: Batch Apex Job Progress Bar
Dynamic Apex in Salesforce
I acknowledge the author for his brilliant work for making this exceptionally useful and informative content to guide us.
ReplyDeleteSalesforce Implementation Services
This is something that you just read and read. You just can’t get tired of it.
ReplyDeleteresumeyard.com
This comment has been removed by the author.
ReplyDeleteGreat work admin!!
ReplyDeleteSalesforce | engineeringway
We cant call future method from batch class we need to use queueable for this.
ReplyDeleteReally we can call future method from execute method of batch class?
ReplyDeletefor calling a future method from batch finish method it giving this error
ReplyDeleteSystem.LimitException: Too many future calls: 1