Batchable Apex Vs Queueable Apex Salesforce VS Future Apex

Batchable Apex

What is Batchable Apex?

  • Batch Apex is used to run large jobs (think thousands or millions of records!) that would exceed normal processing limits.

  • Using Batch Apex, you can process records asynchronously in batches (hence the name, “Batch Apex”) to stay within platform limits.

  • Batch Class should implement Database. Batchable interface and it should have three methods start(), execute() and finish() methods.

When to use a Batchable Apex?

If it is a long-running complex process then you should go for Batchable Apex and you can have an option to schedule the batch to run at a customized time. Or,

If you have a lot of records to process, for example, data cleansing or archiving, Batch Apex is probably your best solution.


What are the limits of Batchable Apex?

  • It can process upto 50 million records in asynchronous mode.

  • 5 concurrent jobs are allowed to run at a time and future methods are not allowed in Batch class.

What is Sample Syntax of Batchable Apex?

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


Future Apex or Future Method

What is future apex or future method?

Future Apex is used to run processes in a separate thread, at a later time when system resources become available.

Note: Technically, you use the @future annotation to identify methods that run asynchronously. However, because “methods identified with the @future annotation” is laborious, they are commonly referred to as “future methods” and that’s how we’ll reference them for the remainder of this module.


When to use a Future Method?

  • For avoiding Mixed_DML_Operations exception, we will be going for the future method where it will be running in asynchronous mode when the system resource becomes available.

  • If you want to perform complex DML operations and want to make external webservice @future(callout=true) means you can go for future method.

Future methods are typically used for:

  • Callouts to external Web services. If you are making callouts from a trigger or after performing a DML operation, you must use a future or queueable method.

  • A callout in a trigger would hold the database connection open for the lifetime of the callout and that is a “no-no” in a multitenant environment.

  • Operations you want to run in their own thread when time permits such as some sort of resource-intensive calculation or processing of records.

What are the limits of Future Method?

  • Future method should be static and return type is void, parameters must be primitive types.

  • Sobjects are not accepted as future method parameters.


What is Sample Syntax of Future Method?

global class SomeClass {
 @future
 public static void someFutureMethod(List<Id> recordIds) {
 List<Account> accounts = [Select Id, Name from Account Where Id IN :recordIds];
 // process account records to do awesome stuff
 }
}
 

Queueable Apex

What is Queueable Apex?

  • It’s similar to the @future method. By using this queueable interface, we can process an Apex that runs for a long time (such as web service call outs and extensive database operations).

  • The job is added to Apex job queue. System.enqueueJob method returns the job id. So, we can easily monitor its progress, either through the Salesforce UI in the Apex Jobs page or by querying the record using AsyncApexJob object

When to use a Queueable Apex?

  • It comes in handy , when you need to have both the operations of Batch and future method and it should implement Queueable Interface.

  • If one job is dependent on other job means here we can chain the dependent job in execute method by system.enqueuejob(new secondJob()).

Benefits

  • Queueable apex supports the Non-primitive type member variables, such as SObject or custom apex types.

  • System.enqueueJob method returns the AsyncApexJob record id.

  • You can chain one job with another job and In Queueable apex you can get Job Id to monitor the job progress.

What are the limits of Queueable Apex?

  • It will accept non-primitive types like sObjects and it also runs in asynchronous mode.

  • You can also able to chain up to 50 jobs and in developer edition, you can able to chain up to 5 jobs only.

What is Sample Syntax of Queueable Apex?

public class AsyncExecutionExample implements Queueable {
 public void execute(QueueableContext context) {
 Account a = new Account(Name='Acme',Phone='(415) 555-1212');
 insert a;        
 }
}
 


3 views0 comments

Recent Posts

See All

Salesforce Configuration Topics

CRM - Customer Relationship Management 1.             What is CRM(Customer Relationship Management)? 2.             CRM Basic entities 3.             Sub Modules of CRM 4.             Existing CRMs Sa

Daily Information | Daliylinx are related to General information, Technology, Salesforce, Newspapers, Telugu Newspapers, Pets, Cars etc

  • Facebook