Asynchronous Apex in Salesforce: A Simple Guide

Blog
Spread the love

In Salesforce development, managing how and when your code runs is crucial for performance and user experience. Two primary ways to handle code execution are Synchronous and Asynchronous processing. Let’s explore these concepts in simple terms.

What Is Synchronous Processing?

Synchronous processing means that tasks are performed one after another, in the order they are called. The system waits for each task to finish before moving on to the next.

Salesforce Example:

When a user clicks “Save” on a Contact record, a trigger runs to validate the email format. After the validation completes, the system proceeds to save the record. If the validation fails, the record isn’t saved. Everything happens in a single thread, and the user waits for the whole process to finish before seeing the confirmation.

What Is Asynchronous Processing?

Asynchronous processing allows tasks to run in the background, independently of the main process. This means the system doesn’t wait for the task to finish and can continue with other operations.

Salesforce Example:

Suppose a user updates an Account, and the system needs to update related Contacts and send emails. Instead of doing all of this immediately (and making the user wait), a Queueable class is called to handle the related updates and email notifications in the background.

What Is a Thread?

A thread is the smallest unit of execution within a program. In Apex, a thread can be seen as a worker that processes a task.

  • Same Thread (Synchronous): All tasks are performed one after another.
  • Separate Threads (Asynchronous): Tasks run in parallel in different threads, allowing background processing.

Same Thread (Synchronous) Processing

Salesforce Scenario:

Imagine a trigger on the Opportunity object that:

  1. Checks if the Opportunity amount is above a threshold.
  2. If yes, it creates a related Approval Request record.
  3. Then it updates the Opportunity Stage to “Pending Approval”.

All these actions happen in sequence, in the same thread, and the user must wait until all operations finish before receiving a response.

Separate Threads (Asynchronous) Processing

Salesforce Scenario: When a user uploads a CSV file of 10,000 leads via a custom Visualforce page, the system:

  1. Accepts the file and returns a success message immediately.
  2. Then, in the background, a Batch Apex job is started to process the 10,000 leads in chunks.
  3. After processing, it sends a notification email to the user.

The upload is quick, and the user doesn’t have to wait for all 10,000 leads to be processed.

Synchronous vs. Asynchronous: A Simple Comparison

Feature Synchronous Asynchronous
Execution Timing Immediate, one after another In the background, independently
User Wait Time User must wait for task completion User can continue without waiting
Performance Slower with heavy tasks Faster for large/long tasks
Use Cases Validations, small updates Batch jobs, sending emails, callouts

When to Use Synchronous Processing

Use synchronous processing when:

  • You need immediate feedback.
  • Example: Updating a Case status and showing the result to the agent immediately.

  • The task is quick and won’t cause delays.
  • Example: Checking field values in a trigger.

  • The task must be completed in a specific order.
  • Example: Inserting a parent record before its children.

When to Use Asynchronous Processing

Use asynchronous processing when:

  • The task is time-consuming.
  • Example: Generating a report with thousands of records.

  • You want to improve performance by offloading tasks.
  • Example: Syncing data with an external system using callouts.

  • The task can run independently.
  • Example: Sending follow-up emails after a campaign member is created.

How to Implement Asynchronous Apex in Salesforce

Salesforce provides several tools for asynchronous processing:

  1. @future methods
  2. Use for simple background tasks, like sending emails or updating related records.

  3. Queueable Apex
  4. Use when you need to chain jobs or pass complex data types.

  5. Batch Apex
  6. Use for large-scale data processing (over 50,000 records).

  7. Scheduled Apex
  8. Use to run tasks at specific times (e.g., nightly cleanup jobs).

Summary

  • Synchronous Processing: Tasks run one after another, blocking other operations.
  • Asynchronous Processing: Tasks run in the background, freeing up the system.
  • Threads: Represent execution paths—synchronous uses one, asynchronous uses multiple.
  • Choosing Wisely:
  • Use synchronous for quick, dependent operations.

    Use asynchronous for heavy, time-consuming, or independent jobs.

bluethinkinc_blog

bluethinkinc_blog

2025-05-22

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

Asynchronous Apex in Salesforce: A Simple Guide

Spread the love In Salesforce development, managing how and

Understanding Database Allows Callout in Salesforce Apex

Spread the love In Salesforce, the Database.AllowsCallouts

PODMAN

Spread the love 1.Introduction Podman: The Modern Container

Running Your Java Program (JAR) 24/7 on AWS EC2 Automatically

Spread the love Introduction Running a Java application

Building a REST API with Spring Boot

Spread the love Introduction REST (Representational State

Custom API to Fetch Customer Data by Customer ID in Spryker

Spread the love Introduction In this blog post, we will

Creating a Custom Module for a Custom Frontend Page in Spryker

Spread the love To create a custom module for a custom

How to create custom module api get category list without access token in Spryker

Spread the love To create a custom API for fetching a

Creating a Custom Glue API in Spryker

Spread the love Spryker’s Glue API is a powerful tool

Get AI Generated 360-Degree View of Account Summary in Dynamics 365 Sales

Spread the love Navigating the demanding landscape of sales

bluethinkinc Blogs