https://github.com/jamessimone/apex-async-processor
Abstract away which async framework is being used within Salesforce in favor of dynamically using the most appropriate solution for the problem at hand.
https://github.com/jamessimone/apex-async-processor
apex async batch queueable salesforce
Last synced: about 2 months ago
JSON representation
Abstract away which async framework is being used within Salesforce in favor of dynamically using the most appropriate solution for the problem at hand.
- Host: GitHub
- URL: https://github.com/jamessimone/apex-async-processor
- Owner: jamessimone
- License: mit
- Created: 2023-01-10T15:16:15.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-19T14:05:42.000Z (9 months ago)
- Last Synced: 2025-02-02T00:51:16.561Z (4 months ago)
- Topics: apex, async, batch, queueable, salesforce
- Language: Apex
- Homepage:
- Size: 26.4 KB
- Stars: 35
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: Contributing.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Apex Async Processor
This repository showcases how to use the `AsyncProcessor` pattern to avoid having to create batch classes and/or queueable classes.
Using this pattern greatly simplifies how asynchronous code is defined and run on platform. Simply:
- clone the repository or copy/paste the [AsyncProcessor](../../blob/main/core/classes/AsyncProcessor.cls) and [AsyncProcessorTests](../../blob/main/core/classes/AsyncProcessorTests.cls) files to your org
- extend the `AsyncProcessor`, defining code you'd like to have processed asynchronously:- also add any additional interfaces you need, like `Database.Stateful` (as is appropriate)
```java
public class AsyncContactProcessorExample extends AsyncProcessor {
protected override void innerExecute(List records) {
List contacts = (List) records;
// do whatever processing here
}
}
```- and then in usage:
```java
// within some other class:
new AsyncContactProcessorExample().get('SELECT Id, Account.Name FROM Contact').kickoff();
// or, alternatively, if you have the records already:
new AsyncContactProcessorExample().get(contacts).kickoff();
```For query-based usages, `AsyncProcessor` will automatically choose whether to batch or enqueue based on the default returned by `Limits.getLimitQueryRows()` - this can also be overridden by providing an alternative implementation of `protected virtual Integer getLimitToBatch()` for subclasses of `AsyncProcessor`.
## Further Examples
Here's an example lowering the `getLimitToBatch()` amount from 50k to 10k records. This could be really useful if the data you're passing in might otherwise blow up the heap size limit.
```java
public without sharing class LowerLimitAsyncProcessor {public override Integer getLimitToBatch() {
return Limits.getLimitDmlRows();
}
}
```You can also look at [ContactAsyncProcessor](../../blob/main/example-app/classes/ContactAsyncProcessor.cls) for a short, complete example.