Transaction Management

Correlating logs across LWC → Apex → Flow boundaries using transaction management for end-to-end traceability.

Transaction Handling & Correlation

  • Start early (user entry points): Triton.instance.startTransaction() (Apex) or triton.startTransaction() (LWC).

  • Resume across hops:

    • Capture the current transactionId in LWC (triton.transactionId) and pass it in Apex method params.

    • In Apex entry, call Triton.instance.resumeTransaction(txId) if provided; otherwise withCache() can auto-resume if configured.

  • Stop & flush at natural boundaries: end of a long-running flow, batch finish, queueable completion, on modal close/navigation in LWC.

Apex (Controller Entry)

@AuraEnabled(cacheable=false)
public static List<Opportunity> loadOpps(Id accountId, String txId) {
    if (String.isNotBlank(txId)) Triton.instance.resumeTransaction(txId);
    else Triton.instance.startTransaction();

    // Set up template for opportunity loading
    Triton.instance.setTemplate(
        Triton.makeBuilder()
            .category(TritonTypes.Category.Apex)
            .type(TritonTypes.Type.Backend)
            .area(TritonTypes.Area.OpportunityManagement)
            .relatedObject(accountId)
    );

    try {
        // ... do work
        Triton.instance.log(
            Triton.instance.fromTemplate()
                .level(TritonTypes.Level.INFO)
                .summary('Loaded opportunities')
                .details('accountId=' + accountId)
        );
        return [SELECT Id, Name, Amount FROM Opportunity WHERE AccountId = :accountId LIMIT 200];
    } catch (Exception e) {
        Triton.instance.log(
            Triton.instance.fromTemplate()
                .exception(e)
        );
        throw e;
    } finally {
        Triton.instance.stopTransaction();
    }
}

LWC (Passing Transaction Across)

Real-World Example: Multi-Step Lead Conversion Process

Transaction ID Flow

Understanding how the transaction ID connects logs across contexts is crucial:

  1. LWC Initialization

    • Direct Params: Transaction ID is generated when first log is created

    • Cache: Transaction ID is generated and automatically cached

  2. Apex Execution

    • Direct Params: Transaction ID is passed via method parameter and resumed

    • Cache: Transaction ID is automatically retrieved from cache

  3. Flow Execution

    • The Flow's Interview GUID is automatically associated with the transaction

    • All Flow logs are grouped under the same transaction

Best Practices

Transaction Lifecycle

  1. Start early: Begin transactions at user entry points

  2. Resume consistently: Always resume transactions in Apex when txId is provided

  3. Stop properly: End transactions at natural boundaries

  4. Handle failures: Ensure transactions are stopped even when errors occur

Correlation Strategies

  1. Direct parameter passing: Explicitly pass transaction IDs for simple scenarios

  2. Platform cache: Use cache for complex, multi-component scenarios

  3. Interview GUID: Always use Flow Interview GUID for Flow correlation

  4. Error handling: Maintain transaction context even during failures

Performance Considerations

  1. Cache availability: Check if platform cache is enabled before using cache-based correlation

  2. Memory management: Be aware of transaction context memory usage in long-running processes

  3. Boundary management: Stop transactions promptly to free resources

By mastering transaction management, you'll create complete execution traces that span across all Salesforce technologies, enabling powerful debugging and monitoring capabilities.

Last updated