You need to sign in to do that
Don't have an account?
Narasimha L
What are the methods in batch apex and which method runs in synchronous mode?
Hi Dev's
Q). What are the methods in batch apex and which method runs in synchronous mode?
Regards
Narasimha
Q). What are the methods in batch apex and which method runs in synchronous mode?
Regards
Narasimha
You can refer this link for methods in batch apex
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
start method is the synchronous method in batch apex
Thank you
Batch Apex is exposed as an interface that must be implemented by the developer. Batch jobs can be programmatically invoked at runtime using Apex.
Need of Batch Apex: - As you all might know about the salesforce governor limits on its data. When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records which satisfies the Governor limits.
But for medium to large enterprises, it is essential to manage thousands of records every day. Adding/editing/deleting them when needed.
Salesforce has come up with a powerful concept called Batch Apex. Batch Apex allows you to handle more number of records and manipulate them by using a specific syntax.
We have to create a global apex class which extends Database.Batchable Interface because of which the salesforce compiler will know, this class incorporates batch jobs. Below is a sample class which is designed to delete all the records of Account object (Let us say your organization contains more than 50 thousand records and you want to mass delete all of them).
Methods in Batch Apex:
Start method
The start method is called at the beginning of a batch Apex job. Use the start method to collect the records or objects to be passed to the interface method execute.
Syntax: global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}
This method returns either a Database.QueryLocator object or an iterable that contains the records or objects being passed into the job.
Execute Method
The execute method is called for each batch of records passed to the method. Use this method to do all required processing for each chunk of data.
Syntax: global void execute(Database.BatchableContext BC, list<P>){}
This method takes the following:
o A reference to the Database.BatchableContext object.
o A list of sObjects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, the returned list should be used.
Batches of records execute in the order they are received from the start method.
Finish Method
Syntax: global void finish(Database.BatchableContext BC){}
The finish method is called after all batches are processed. Use this method to send confirmation emails or execute post-processing operations.
Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter from Database.executeBatch is considered five transactions of 200 records each.
The Apex governor limits are reset for each transaction. If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back.
Note: As batch apex runs in asynchronous mode of execution all the methods in batch apex runs in asynchronous mode.
Please mark this as best answer if this helps.
Best Regards,
Nagendra.
//Database.Stateful: Maintain state of variables Eg: PositionBatch
//records to be processed
public Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator([Select Id from Account]);
}
//login that will be applied to each records in the scope
global void execute(Database.BatchableContext info, List<SObject> scope){
}
//logic that will run once
global void finish(Database.BatchableContext bc){
}
}
The batch runs in Asyschronous mode and so does all the three methods.
All execute methods for batches within a batch job are synchronous so they will not "trip" over each other.
Regards,