Skip to content

UTIL_AsyncChain.ChainContext

Class

apex
global inherited sharing class UTIL_AsyncChain.ChainContext

Shared 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:

apex
context.put('accountId', account.Id);
Id accountId = (Id)context.get('accountId');
Boolean hasKey = context.has('accountId');

Methods

MethodDescription
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

apex
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:

apex
Object value = context.get('recordCount');

getAs

apex
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:

  • key (String) - The key to look up.
  • targetType (Type) - The Apex Type to deserialize the value into.

Returns: Object - The deserialized value, or null if the key is not found.

Since: 1.0

Example:

apex
List<String> names = (List<String>)context.getAs('nameList', List<String>.class);

getChainExecutionId

apex
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:

apex
String executionId = context.getChainExecutionId();

getCorrelationId

apex
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:

apex
String correlationId = context.getCorrelationId();

getCurrentStepIndex

apex
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:

apex
String idempotencyKey = context.getChainExecutionId() + '-' + context.getCurrentStepIndex();

getPreviousStepResult

apex
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:

apex
UTIL_AsyncChain.StepResult previous = context.getPreviousStepResult();
if(previous != null && previous.success)
{
    String message = previous.message;
}

has

apex
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:

apex
if(context.has('accountId'))
{
    Id accountId = (Id)context.get('accountId');
}

put

apex
global void put(String key, Object value)

Stores a value in the context under the given key.

Parameters:

  • key (String) - The key to store the value under.
  • value (Object) - The value to store.

Since: 1.0

Example:

apex
context.put('batchSize', 200);