Knowing when to use what

Asynchronous Apex, in its various forms, is incredibly useful. By trading synchronous execution of your code for relaxed or reset governor limits, you can process massive numbers of records, call out to web services without holding up your users, and, as of the Spring '15 release, you can even write fault tolerant asynchronous callouts. Knowing when to use which type of Asynchronous Apex, however, can be difficult. With that in mind, here are some guidelines:

  • If you are manipulating more than 2000 records, use Batchable Apex.
  • While you can directly schedule Batchable Apex, it is almost always preferable to wrap the batch Apex execution in a scheduled class, as this provides much more flexibility regarding execution time and.
  • Use the @future annotation in situations where the method can be called and forgotten.
  • Default to Queueable Apex for asynchronous code, manipulating less than 2000 records at a time.
  • Write and use a standard wrapper for Queueable Apex like the one we discussed previously to create fault-tolerant queues.

These guidelines are just guidelines, and each individual situation should be evaluated to determine the appropriate Asynchronous Apex pattern. Remember that each of these Asynchronous tools has their own set of tradeoffs and benefits. They all run when the system has resources available, but the @future methods, for instance, can only accept primitive data types and cannot be chained. On the other hand, batchable classes can be chained, and can handle larger sets of data. Schedulable Apex provides the most flexible way to repeatedly run asynchronous Apex with an incredibly flexible cron scheduler. Finally, Queueable Apex, while new, is easily the most flexible, especially with a wrapper class controlling it. Queueable Apex additionally provides the only fault-tolerant asynchronous solution for situations like callouts where the work must be completed, even if it takes a few tries!

Asynchronous Apex is easily one of the most powerful tools Force.com developers have at their disposal. Developers have to master the various asynchronous Apex techniques to unlock the true power of the platform. With asynchronous Apex machine learning, data mining and deep custom integrations with other systems are possible. Hopefully, this chapter has laid out a good understanding of the various patterns, but the real mastery of these patterns will come through practice and experience.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.10