What Can We Do With Salesforce Asynchronous Apex?

Jiwei Feng
TeamSpirit Engineering
3 min readSep 1, 2020

Apex provides four ways to run tasks asynchronously. What are they and how can we use them? We will explore more in this post!

Key Reference: Apex developer guide for asynchronous apex

1. Use apex scheduler to run programs on schedule

This is quite straightforward.

A schedule can be written in cron expression or can be setup through UI. Usually, we use the scheduler to trigger batch jobs to run overnight.

2. Use future methods to run tasks in the background

The future method is very suitable to run lightweight background jobs.

Fact Check:

  1. Future methods must be declared as public static void fn with primitiveparameters only.
  2. You can call 50 future methods from each apex transaction but 0 from batch job.
  3. According to Execution Governors and Limits, there is only one future method allowed from queueable. However, I have tested calling two in a perfectly fine manner.
  4. So I suggest doing some practice to make sure your understanding is correct. Salesforce documentation is not always 100% accurate!
  5. No chaining allowed for future methods.
Future method

3. Use Queueable for a more complex job

Queueable

Fact Check:

  1. Queueable can use non-primitive parameters (by instance variables).
  2. Batch job can submit one queueable job.
  3. You can chain queueable job one by one.
  4. If you submit another queueable job in the previous job’s constructor, you have the chance to trigger a list of queueable jobs in a parallel manner. But this kind of execution is not always successful.
  5. Chaining depth is determined by org type. For developer edition and trial org, the depth is 5. Others can have unlimited depth.

4. Use batch for long time process and large amount records

Batch

Fact Check:

  1. In batch, you can use Database.QueryLocator to bypass the governor limit.
  2. Each execution of a batch Apex job is considered a discrete transaction. If you set scope = 1 when executing batch, the whole governor limit will be applied for only one record in one transaction. In this way, you can maximize the processing ability if your batch job depends on external services.
  3. You can use stateful batch to keep states of the instance variables.
  4. Usually, we only chain batch jobs in the finish method one by one. But if you have an extremely long execution time batch job, you can run them in parallel for up to five jobs together.

However, when you split batch jobs in parallel, there is a possibility you will get record lock errors due to salesforce auto-locking mechanism. In this case, you need to carefully scale the target data or use special handling (such as cloning related records) to solve the conflicts.

But you can reduce 80% processing time! Even though this is a trade-off, it is worth trying!

At TeamSpirit, we actively use features and technologies from Salesforce to solve problems from our customers and generate business value.

If you have an interest in Salesforce or any question regarding this article, feel leave a comment!

--

--