For the complete documentation index, see llms.txt. This page is also available as Markdown.

Platform Cache for Transactions

Use Salesforce Platform Cache to share a transaction id across contexts — correlate LWC and Apex logs without passing the id by hand.

LWC and Apex threaded the transaction id through a method parameter. That's clean when you control the call and there's an obvious place to put it. But sometimes there isn't — multiple components, async hops, a chain of calls where plumbing an id through every signature is a pain. For those cases, Platform Cache can hold the id for you, and both sides just ask for it.

Prerequisite: you need a default cache partition with an Org cache allocation. See Platform Cache Setup in the install guide.

The idea

Instead of passing the id, you call withCache(). Triton stores the transaction id in Platform Cache keyed by the running user, and any other context that calls withCache() for the same user picks it up. No parameter required.

LWC side

Notice there's no transactionId argument — the component just flags that caching is on:

logDemo.js
export default class LogDemo extends LightningElement {
    triton;

    connectedCallback() {
        this.triton = new Triton().bindToComponent('LogDemo');
    }

    async executeScenario() {
        await this.triton.logNow(
            this.triton.debug(TYPE.FRONTEND, AREA.ACCOUNTS)
                .summary('Cache Demo - Start')
                .details('Starting execution with cache enabled')
        );

        try {
            // No transactionId passed — the cache carries it
            await apexActionThatGeneratesDMLError({ cacheEnabled: true });
        } catch (error) {
            await this.triton.logNow(this.triton.exception(error));
        }
    }
}

Apex side

One call — Triton.withCache() — and the Apex logs adopt the cached transaction id:

How withCache() works

When you call it, Triton runs this logic:

  1. Check whether Platform Cache is available and configured.

  2. If it is: build a cache key from the running user's id, look for an existing transaction id, resume it if found, or start a new one and cache it if not.

  3. If it isn't: fall back to starting a normal transaction and carry on.

That automatic fallback is the nice part — you never have to special-case an org without cache configured. withCache() just does the right thing.

When to use it — and when not to

Good fits

  • Multiple components or services that should share one trace.

  • Correlating across asynchronous boundaries where parameter passing is awkward.

  • Reducing signature clutter in a deep call chain.

Caveats

Won't work with cacheable Apex / @wire

Rules of thumb

  • Use parameter passing for simple, synchronous, single-hop calls (the previous page).

  • Use parameter passing for cacheable Apex / @wire — do not rely on withCache() there.

  • Use cache for genuinely complex, multi-context scenarios where signatures are awkward and the Apex is not cacheable.

  • Reach for startTransaction() when you need a clean boundary between operations.

Platform cache is a sharp tool — powerful for the hard cases, overkill for the easy ones. Pick based on how tangled your call graph actually is. For the bigger picture on stitching traces across every layer, see Transactions & Cache and End-to-End Logging.

Last updated