UTIL_AsyncChain.ChainContext
Class
global inherited sharing class UTIL_AsyncChain.ChainContextShared state container passed between chain steps. Provides key-value storage for inter-step communication and access to chain metadata such as execution ID and correlation ID.
Since: 1.0
Example:
context.put('accountId', account.Id);
Id accountId = (Id)context.get('accountId');
Boolean hasKey = context.has('accountId');Methods
| Method | Description |
|---|---|
| global Object get(String key) | Retrieves a value from the context by key. |
| global Object getAs(String key, Type targetType) | Retrieves a value from the context and deserializes it to the specified type. |
| global String getChainExecutionId() | Returns the ID of the AsyncChainExecution__c record tracking this chain. |
| global String getCorrelationId() | Returns the correlation ID for this chain, used for log correlation across transactions. |
| global Integer getCurrentStepIndex() | Returns the zero-based index of the currently executing step. |
| global UTIL_AsyncChain.StepResult getPreviousStepResult() | Returns the result of the most recently completed step. |
| global Boolean has(String key) | Checks whether the context contains a value for the given key. |
| global void put(String key, Object value) | Stores a value in the context under the given key. |
Method Details
get
global Object get(String key)Retrieves a value from the context by key.
Parameters:
key(String) - The key to look up.
Returns: Object - The stored value, or null if not found.
Since: 1.0
Example:
Object value = context.get('recordCount');getAs
global Object getAs(String key, Type targetType)Retrieves a value from the context and deserializes it to the specified type. Useful for retrieving complex objects that were stored via put() and serialized between transactions.
Parameters:
Returns: Object - The deserialized value, or null if the key is not found.
Since: 1.0
Example:
List<String> names = (List<String>)context.getAs('nameList', List<String>.class);getChainExecutionId
global String getChainExecutionId()Returns the ID of the AsyncChainExecution__c record tracking this chain.
Returns: String - The chain execution record ID.
Since: 1.0
Example:
String executionId = context.getChainExecutionId();getCorrelationId
global String getCorrelationId()Returns the correlation ID for this chain, used for log correlation across transactions.
Returns: String - The correlation ID string.
Since: 1.0
Example:
String correlationId = context.getCorrelationId();getCurrentStepIndex
global Integer getCurrentStepIndex()Returns the zero-based index of the currently executing step. Useful for building idempotency keys (e.g., executionId + stepIndex).
Returns: Integer - The current step index, or null for contexts not yet assigned to a step.
Since: 1.0
Example:
String idempotencyKey = context.getChainExecutionId() + '-' + context.getCurrentStepIndex();getPreviousStepResult
global UTIL_AsyncChain.StepResult getPreviousStepResult()Returns the result of the most recently completed step. Returns null for the first step.
Returns: UTIL_AsyncChain.StepResult - The previous step's StepResult, or null if this is the first step.
Since: 1.0
Example:
UTIL_AsyncChain.StepResult previous = context.getPreviousStepResult();
if(previous != null && previous.success)
{
String message = previous.message;
}has
global Boolean has(String key)Checks whether the context contains a value for the given key.
Parameters:
key(String) - The key to check.
Returns: Boolean - True if the key exists in the context, false otherwise.
Since: 1.0
Example:
if(context.has('accountId'))
{
Id accountId = (Id)context.get('accountId');
}put
global void put(String key, Object value)Stores a value in the context under the given key.
Parameters:
Since: 1.0
Example:
context.put('batchSize', 200);