0% found this document useful (0 votes)
20 views

Material 08

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Material 08

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TOPIC 08: Asynchronous Apex Overview:

1. Introduction to Asynchronous Apex


Overview:
 Definition: Asynchronous Apex refers to the execution of Apex code that
runs in the background without the need for the user to wait for the process
to complete. This is useful for long-running operations that can be
performed without immediate feedback.
 Types of Asynchronous Apex:
o Future Methods: Execute long-running operations in the
background.
o Batch Apex: Handle large volumes of records by processing them in
chunks.
o Queueable Apex: Similar to Future methods but with more complex
job chaining and monitoring capabilities.
o Scheduled Apex: Run Apex classes at specific times, like cron jobs.
o Apex Callouts: Make asynchronous web service callouts.
Characteristics:
 Non-blocking: Does not block the user interface, allowing other operations
to proceed.
 Transactional: Each asynchronous operation runs in its own transaction.
 Governor Limits: Different limits apply compared to synchronous Apex,
such as higher heap size and longer CPU time.
2. When to Use Asynchronous Apex
Use Cases:
 Long-Running Processes: Operations that require significant processing
time, such as complex calculations or data processing.
 Large Data Volumes: Handling thousands or millions of records, which
would exceed the governor limits in synchronous Apex.
 Web Service Callouts: Making callouts to external systems that may take
time to respond.
 Scheduled Jobs: Automating routine tasks like data cleanup or sending
notifications at specific intervals.
 Batch Processing: Processing large datasets in manageable chunks.
Advantages:
 Scalability: Efficiently handles large data volumes by processing them
asynchronously.
 Performance: Improves user experience by offloading time-consuming tasks
to the background.
 Flexibility: Allows chaining of complex operations that may depend on the
completion of previous tasks.
Example Scenarios:
 Future Methods: Sending email notifications after a record is updated
without making the user wait.
 Batch Apex: Processing millions of records to update a field across all
records.
 Queueable Apex: Chaining jobs where the result of one operation is needed
for the next.
 Scheduled Apex: Running daily reports or data synchronization at midnight.
3. Overview of Asynchronous Methods
1. Future Methods:
 Syntax: Mark a method with the @future annotation to run asynchronously.
 Example:

public class FutureExample {


@future
public static void sendEmail(Set<Id> contactIds) {
// Code to send emails
}
}

 Use Cases: Callouts to external services, operations that don't need


immediate user feedback.
2. Batch Apex:
 Components: Batch classes implement Database.Batchable interface with
three methods: start, execute, and finish.
Example:

public class BatchExample implements Database.Batchable<SObject> {


public Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([SELECT Id FROM Account]);
}
public void execute(Database.BatchableContext BC, List<Account> scope) {
// Process each batch of records
}
public void finish(Database.BatchableContext BC) {
// Final steps after all batches are processed
}
}

 Use Cases: Large data processing tasks like data cleansing, mass updates.
3. Queueable Apex:
 Features: Supports job chaining, more complex than Future methods.
 Example:

public class QueueableExample implements Queueable {


public void execute(QueueableContext context) {
// Code to execute asynchronously
}
}

 Use Cases: Complex background processing where the results of one job are
needed by another.
4. Scheduled Apex:
 Scheduling: Use the System.schedule method to run Apex at a specific time.
 Example:

public class ScheduledExample implements Schedulable {


public void execute(SchedulableContext SC) {
// Code to execute at the scheduled time
}
}

 Use Cases: Automating repetitive tasks, scheduled data syncs.


5. Apex Callouts:
 Asynchronous Callouts: External web service calls that return responses
asynchronously.
 Example:

@future(callout=true)
public static void makeCallout() {
// Code for making an HTTP callout
}

 Use Cases: Interacting with external APIs, fetching or sending data to third-
party systems.

Thank You

You might also like