Wednesday 29 July 2020

Episode 6-Asynchronous Apex-Future vs Queueable vs Batch Apex in Salesforce




In this blog, we will learn about Asynchronous Apex and the comparison between its types ie. 
Future Methods, Batch Apex, and Queueable Apex. After reading the blog you will be able to
decide when and how to use them. Before wasting time lets get started.😃


Asynchronous Apex

Asynchronous means process which runs in the background without any user inputs or intervention. As Salesforce has the multi-tenant architecture, using resources efficiently is always the big challenge. Therefore we have the concept of governor limits which ensure no one monopolizes the usage of resources. But sometimes we have to process the records in bulk and also to use the platform to its maximum limit. To overcome this challenge, we have the concept of an Asynchronous apex. It not only provides more governor limits in most of the cases but also helps in processing up to 50 million records without hitting the governor limits. Isn't it Amazing?

Future Methods

  • When to use: It is generally used when we want to make a callout to external web service (@future(callout=true ) ) or to prevent the mixed DML error.
  •  The future method should be static, parameters must be primitive types and its return type is void. It does not accept sObjects in its parameter list.
  • Please check my earlier detailed blog on Future Methods.

Queueable Apex

  • When to use: It is generally used when one job is dependent on other .i.e in the chaining of jobs and also when we have to monitor jobs using job id.
  • The above features are not present in future methods.
  • In order to make the class queueable, it should implement a queueable interface.
  • When the jobs are queued it returns an id that is used to monitor the progress and status of the job. In order to add jobs in the queue we have to use System.enqueueJob  as given below:
  • Id jobId = System.enqueueJob(new QueueableClass());
  • When one job is dependent on another job, we chain dependent jobs in execute method using System.enqueueJob(new dependentJob());
  • It is also similar to future methods and gets executed when the resources are available. but it also accepts non-primitive types like sObjects.

Batchable Apex:

  • When to use: It is generally used when we have to process the records in bulk and have to query on large sets of data like in database maintenance work. 
  • It helps in processing up to 50 million records without hitting the governor limits in asynchronous mode.
  • Apex class should implement a Database.Batchable interface having  three methods:start(),execute() and finish().
  • Future methods cannot be called in the batch apex class.
  • You can also schedule the batch apex class to run at the specified time set by the user.
  • You can run 5 concurrent batch jobs at a time.
  • Check my detailed blog on  Batch Apex Blog

Thanks for reading my blog. Keep Learning and sharing your knowledge.
Stay tuned to learn some more concepts and their implementation in salesforce.






  







 


6 comments:

  1. Very useful article. Really helpful.

    ReplyDelete
  2. Thanks. Follow my blog to stay tuned.Many More to come.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site. As a result of checking through the net and meeting techniques that were not productive, Same as your blog I found another one Oracle APEX .Actually I was looking for the same information on internet for Oracle APEX and came across your blog. I am impressed by the information that you have on this blog. Thanks once more for all the details.

    ReplyDelete
  5. superb all points covered at a glance

    ReplyDelete

If you have any doubts ,Please let me know.

Episode 13 : RollUp Summary Trigger for Lookup Relationship

 As we all know we can use roll-up summary field logic only for the master-detail relationship, but in case you have the business requiremen...