Friday 8 December 2017

Batch Apex to Future Methods

Batch Apex:

Batch Apex used to process large volume (50 Million) of data asynchronously.
The maximum number of batch apex method executions per a 24 hour period is 2,50,000
Call outs upper limit is 100

Batch Apex Constructor VS Start Method ?
Batch class must implement Database.Batchable interface,Must have implementation for the start,execute,finish methods.
If requirement is to pass the parameters to Start method that can be done via Constructor.
Always constructor executes 1st.

Ex: Process only records created by specific user,Then that user id can be passed to Constructor from there will be assigned to local variable and that can be used in Start method.

Future Method :

Future methods also executes asynchronously,If any long running operation needs to be performed and don't want to wait for it to get complete then that logic can be written in future methods.

Is it possible to call future method from Batch Apex?

No,Both are asynchronous calls so its not allowed to call from Batch Apex.

Any other alternate way to implement this?

Yes,Only from finish method can be called.

If we try to call from execute method ,We will not get any compilation error but in run time we will get below error
"First error: Future method cannot be called from a future or batch method: DT_Utility.sendEmail()"

Ex :

Create a future method to send email notification :




Try calling from Batch Apex Execute method :



Run the batch job :


Database.executeBatch(new BatchApexUnderstanding());


Apex Job Status :






No comments:

Post a Comment