UTIL_AsynchronousJobLauncher
Class · Group: Async Processing
Learn it: Async Processing guide
global inherited sharing class UTIL_AsynchronousJobLauncherProvides a simplified, static entry point for running complex asynchronous jobs using the UTIL_AdaptiveAsynchronousProcessor engine.
Example
List<Object> items = new List<Object>{ 'item1', 'item2', 'item3' };
IF_Async.Processable processor = new MyCustomProcessor();
Id jobId = UTIL_AsynchronousJobLauncher.process(items, processor, 50);See Also: IF_Async
Methods
| Method | Description |
|---|---|
| global static Id process(List<Object> items, IF_Async.Processable processor) | Starts an adaptive asynchronous job with automatic execution strategy selection. |
| global static Id process(List<Object> items, IF_Async.Processable processor, Integer jobSize) | Starts an adaptive asynchronous job with a custom job size for batch or queueable processing. |
| global static Id process(UTIL_AsynchronousJobLauncher.DTO_AsynchronousJobRequest request, IF_Async.Processable processor) | Starts an adaptive asynchronous job with full configuration control via a request object. |
process
global static Id process(List<Object> items, IF_Async.Processable processor)Starts an adaptive asynchronous job with automatic execution strategy selection. The framework chooses between Queueable and Batch Apex for you. If the code already runs inside an asynchronous job (a Queueable, Batch or future method), it starts a chain of Queueables, whatever the number of items. Otherwise, more items than the batch threshold run as Batch Apex, and a number of items equal to or below the threshold runs as Queueables. When no Queueable job can be added in this transaction, it uses Batch Apex.
Parameters
| Parameter | Type | Description |
|---|---|---|
items | List | The Items to process |
processor | Object | An instance of a class that implements Processable. |
Returns Id — The ID of the started asynchronous job.
Throws
| Exception | Description |
|---|---|
| Exception | Salesforce refused to start a job. Catch it as Exception. For a queueable job, getCause() returns the error Salesforce raised. |
Example
// Process a list of accounts using a custom processor
List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 100];
IF_Async.Processable processor = new MyAccountProcessor();
Id jobId = UTIL_AsynchronousJobLauncher.process(accounts, processor);
// jobId is the Id of the job that was startedglobal static Id process(List<Object> items, IF_Async.Processable processor, Integer jobSize)Starts an adaptive asynchronous job with a custom job size for batch or queueable processing. The job size controls how many records are processed in each batch execution or queueable transaction.
Parameters
| Parameter | Type | Description |
|---|---|---|
items | List | The Items to process |
processor | Object | An instance of a class that implements Processable. |
jobSize | IF_Async.Processable | The size of each job to be executed |
Returns Id — The ID of the started asynchronous job.
Throws
| Exception | Description |
|---|---|
| Exception | Salesforce refused to start a job. Catch it as Exception. For a queueable job, getCause() returns the error Salesforce raised. |
Example
// Process contacts with a custom batch size of 50
List<Contact> contacts = [SELECT Id, Email FROM Contact WHERE Email != null LIMIT 500];
IF_Async.Processable processor = new EmailValidationProcessor();
Id jobId = UTIL_AsynchronousJobLauncher.process(contacts, processor, 50);
// Up to 500 contacts are processed 50 at a time; jobId is the Id of the job that was startedglobal static Id process(UTIL_AsynchronousJobLauncher.DTO_AsynchronousJobRequest request, IF_Async.Processable processor)Starts an adaptive asynchronous job with full configuration control via a request object. This method provides maximum flexibility, allowing you to configure batch size, queueable job size, execution strategy, and other advanced settings through the DTO_AsynchronousJobRequest object. A processor that implements IF_Async.Finishable cannot be forced to run as PARALLEL_QUEUEABLES: its finish step would run once for every job. That request is refused before any job is started.
Parameters
| Parameter | Type | Description |
|---|---|---|
request | UTIL_AsynchronousJobLauncher.DTO_AsynchronousJobRequest | The AsynchronousJobRequest containing the data and configuration. |
processor | IF_Async.Processable | An instance of a class that implements Processable. |
Returns Id — The ID of the started asynchronous job.
Throws
| Exception | Description |
|---|---|
| Exception | Salesforce refused to start a job. Catch it as Exception. For a queueable job, getCause() returns the error Salesforce raised. |
| IllegalArgumentException | The request forces PARALLEL_QUEUEABLES and the processor implements IF_Async.Finishable. |
Example
// Process opportunities with advanced configuration
List<Opportunity> opportunities = [SELECT Id, Amount FROM Opportunity WHERE StageName = 'Closed Won' LIMIT 1000];
DTO_AsynchronousJobRequest request = new DTO_AsynchronousJobRequest(opportunities)
.withBatchSize(100)
.withQueueableJobSize(50)
.withExecutionStrategy(IF_Async.AsynchronousExecutionStrategy.BATCH);
IF_Async.Processable processor = new OpportunityProcessor();
Id jobId = UTIL_AsynchronousJobLauncher.process(request, processor);
// jobId is the Id of the job that was startedInner Classes
| Class | Description |
|---|---|
| DTO_AsynchronousJobRequest | Request object for initiating an asynchronous process. |