Welcome to the Custom Async Framework for Salesforce Apex! This project provides an alternative to Salesforce's built-in Queueable and Batch Apex classes, leveraging Platform Cache and Platform Events to achieve asynchronous processing in Salesforce org.
Salesforce provides powerful tools for asynchronous processing like Queueable and Batch Apex. However, there are scenarios where additional control and customization are required, and this is where the Custom Async Framework comes into play. The framework utilizes Platform Cache to store class instances and Platform Events to execute actions in a sequence, making it a reliable and efficient solution for asynchronous processing needs.
- Alternative to Queueable and Batch Apex.
- Utilizes Salesforce Platform Cache for storing class instances, reducing the overhead of object initialization.
- Leverages Platform Events as an event bus for executing actions in a sequence.
- Easy-to-use and intuitive API for developers.
To use the Custom Async Framework in your Salesforce org, follow these steps:
- Clone the repository to your local machine or download the latest release.
- Deploy the components to your Salesforce org using the Salesforce CLI or any other deployment tool.
Using the Custom Async Framework is straightforward. Here's a quick guide:
To utilize the Custom Async Framework for a Queue type job, follow this example:
- Create a Queue class that implements the
Async.Queue
interface:
public with sharing class MyQueueJob implements Async.Queue {
private String message;
public MyQueueJob(String message) {
this.message = message;
}
public void execute() {
// Perform your asynchronous operations here...
System.debug('Executing Queue Job with message: ' + message);
// Add custom logic as needed...
}
}
- Enqueue the job using the
EnqueueJobs
class:
String myMessage = 'Hello, from the Custom Async Framework!';
Async.Queue myQueueJob = new MyQueueJob(myMessage);
EnqueueJobs.Enqueue(myQueueJob);
- Create a Batch class that implements the
Async.Batch
interface:
public without sharing class MyBatchJob implements Async.Batch, Async.Stateful {
Integer recordsProcessed = 0;
public Iterable<Sobject> start() {
return new IterableSobject(
'SELECT Id, Name FROM MyCustomObject__c WHERE Status__c = \'Pending\' LIMIT 200'
);
}
public void execute(List<SObject> scope) {
// Process each batch of records
for (SObject record : scope) {
// Add your batch processing logic here...
record.put('Status__c', 'Processed');
recordsProcessed++;
}
update scope;
}
public void finish() {
System.debug(recordsProcessed + ' records processed. Batch job complete!');
}
}
- Enqueue the Batch job
String jobId = EnqueueJobs.Batch(new MyBatchJob(), 100);
The Custom Async Framework utilizes the following interfaces for defining asynchronous jobs:
The Batch
interface defines the structure of a Batch Apex job.
public Interface Batch {
Iterable<sObject> start();
void execute(List<SObject> scope);
void finish();
}
The Queue
interface defines the structure of a Queueable job.
public Interface Queue {
void execute();
}
The Stateful
interface serves as a marker interface to indicate that a Batch Apex job should maintain its state.
public Interface Stateful {
// Marker Interface
}
The Custom Async Framework is composed of several components, each serving a specific purpose. Here are the key components included in this framework:
The Platform Cache setup is essential for utilizing the Custom Async Framework effectively. Ensure that you have set up the Platform Cache as described in the package.
The Platform Event setup is essential for handling asynchronous events in the Custom Async Framework. Ensure that you have set up the Platform Event and the associated trigger as described in the package.
The EnqueueJobs
class is responsible for enqueuing asynchronous jobs, either Queueable or Batch.
The AsyncJobTriggerHandler
class is responsible for processing Queue and Batch jobs triggered by Platform Events.
While the Custom Async Framework offers an alternative for asynchronous processing in Salesforce Apex, it also has many limitations that developers should be aware of:
-
No Direct Callouts: Framework does not support making callouts for obvious reasons.
-
No Querylocator: Framework does not support using the
Database.getQueryLocator()
method within Batch type jobs. If your use case requires this feature, please be aware that it is not compatible with the framework's current implementation. -
Limited Number of Records: In Batch jobs, the number of records returned by the
start
method should not exceed 100 KB. -
Platform Cache Limits: The usage of Platform Cache is subject to its own limits and allocations in your Salesforce org. Ensure that you monitor the cache usage to avoid reaching the limits.
-
Governor Limits: As with any Salesforce Apex code, the Custom Async Framework is subject to governor limits. Ensure that your asynchronous jobs are designed to comply with these limits.
Contributions to the Custom Async Framework are welcome!
This project is licensed under the MIT License.