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:
- Checks if the Opportunity amount is above a threshold.
- If yes, it creates a related Approval Request record.
- 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:
- Accepts the file and returns a success message immediately.
- Then, in the background, a Batch Apex job is started to process the 10,000 leads in chunks.
- 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.
- The task is quick and won’t cause delays.
- The task must be completed in a specific order.
Example: Updating a Case status and showing the result to the agent immediately.
Example: Checking field values in a trigger.
Example: Inserting a parent record before its children.
When to Use Asynchronous Processing
Use asynchronous processing when:
- The task is time-consuming.
- You want to improve performance by offloading tasks.
- The task can run independently.
Example: Generating a report with thousands of records.
Example: Syncing data with an external system using callouts.
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:
- @future methods
- Queueable Apex
- Batch Apex
- Scheduled Apex
Use for simple background tasks, like sending emails or updating related records.
Use when you need to chain jobs or pass complex data types.
Use for large-scale data processing (over 50,000 records).
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
2025-05-22