Asynchronous Programming in Apex (Batch Apex in Salesforce)


Below are the Asynchronous Programming Method in Salesforce Apex 

1. Batch Programming (Batch Apex)
2. Schedule Programming (Schedule Apex)
3. Future Methods
4. Flex Queues
5. Queuable Apex.

 Batch Programming (Batch Apex)
  • Page Layout : 1
  • Import Wizard : 50,000 (Insert, Update)
  • Data Loader : 50,00,000 (Import & Export) 
  • Batch Apex: 50 Million Records (5 Crore)
  • Third Party Tools / Salesforce Partners : More than 50 Million

What is Batch Apex : 

  • Batch Apex is Asynchronous execution of Apex code, specially designed for processing a large number of records, and has greater flexibility in governor limits than the Synchronous code.

When to use Batch Apex :
  • When you want to process a large number of records on daily basis or even at a specific time of interval then you could go for Batch Apex.
  • Also, when you want an operation to be asynchronous then you could implement the Batch Apex. Batch Apex is exposed as an interface that must be implemented by the developer. Batch jobs can be programmatically invoked at runtime using Apex. 
  • Batch Apex operates over small batches of records, covering your entire recordset and breaking the processing down to manageable chunks of data.

Use of Batch Apex
  • Several Apex Governor limits can be conquered using batch apex as it breaks down a Job into smaller jobs. It is useful when you require to query thousand of records and perform DML operations on them without hitting the governor limits.
Batch Apex Syntax 
  • To write a Batch Apex class, your class must implement the Database.Batchable interface and include the following three methods:

start
  • Used to collect the records or objects to be passed to the interface method execute for processing.
  • This method is called once at the beginning of a Batch Apex job and returns either a Database.QueryLocator object or an Iterable that contains the records or objects passed to the job.
  • With the QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed and you can query up to 50 million records. However, with an Iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.
execute

  • Performs the actual processing for each chunk or “batch” of data passed to the method. The default batch size is 200 records. Batches of records are not guaranteed to execute in the order they are received from the start method.

This method takes the following:
  • A reference to the Database.BatchableContext object.
  • A list of sObjects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, use the returned list.
finish
  • Used to execute post-processing operations (for example, sending an email) and is called once after all batches are processed.

The skeleton of a Batch Apex class


public class MyBatchClass implements Database.Batchable<sObject> { public (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) { // collect the batches of records or objects to be passed to execute } public void execute(Database.BatchableContext bc, List<P> records){ // process each batch of records } public void finish(Database.BatchableContext bc){ // execute any post-processing operations } }

Example for Batch Class
Example 1: Create a Batch Process, to update All the Contact Records' Title, Mobile, and Fax Number values.



Example 2: Updates the associated contacts with their account’s mailing address.




Invoking a Batch Class
  • By using Execute Anonymous Window
  • By using "Another Batch Job".
  • By using Scheduling 
  • By using VisualForce Pages.
To invoke a batch class, simply instantiate it and then call Database.executeBatch with the instance:
MyBatchClass myBatchObject = new MyBatchClass(); Id batchId = Database.executeBatch(myBatchObject);
You can also optionally pass a second scope parameter to specify the number of records that should be passed into the execute method for each batch. Pro tip: you might want to limit this batch size if you are running into governor limits.
Id batchId = Database.executeBatch(myBatchObject, 100);
Like our post & bookmark this blog for your future learning. 

Any suggestions and improvements are most welcome, please comment your suggestions and improvement in the comment box. 

Happy Coding Sharing Is caring. 😀😀😀 

5 تعليقات

  1. Thank you Khumed for sharing - Asynchronous Programming in Apex. Just yesterday I came across the concepts of synchronous and Asynchronous programming in Apex and you have already posted the required data. Keep Sharing, it boosts us a lot.

    ردحذف
  2. Thanks for the comment will update more in future

    ردحذف

إرسال تعليق

Post a Comment

أحدث أقدم