LWC, Apex and Flows
In this article we will take a look at a more complex logging scenario involving an LWC component, a corresponding Apex controller, and an autolaunched Flow.
In this article, we'll explore a more complex logging scenario involving three Salesforce technologies: an LWC component, its Apex controller, and an autolaunched Flow. Our goal is to produce a unified log trace across all three layers while demonstrating both direct parameter passing and cache-based approaches for transaction management.
The scenario follows this sequence:
User clicks a button in LWC
Apex controller performs a DML operation
Autolaunched Flow triggers from the DML
Flow performs some operations and logs its progress
Implementation Approaches
Let's examine two different approaches to managing transaction IDs across these contexts, with separate implementations for each.
Approach 1: Direct Parameter Passing
This approach explicitly passes the transaction ID through method parameters.
LWC Controller
Apex Controller
Approach 2: Using Platform Cache
This approach uses platform cache to automatically share the transaction ID.
LWC Controller
Apex Controller
Transaction ID Flow
Understanding how the transaction ID connects logs across contexts is crucial:
LWC Initialization
Direct Params: Transaction ID is generated when first log is created
Cache: Transaction ID is generated and automatically cached
Apex Execution
Direct Params: Transaction ID is passed via method parameter and resumed
Cache: Transaction ID is automatically retrieved from cache
Flow Execution
The Flow's Interview GUID is automatically associated with the transaction
All Flow logs are grouped under the same transaction
Here's what the resulting log structure looks like in Pharos:
Apex Controller
The Apex controller can handle both approaches:
The Flow
The Flow setup remains similar to what's detailed in Flows, with one key difference - we need to ensure the Flow can access the same transaction context regardless of which approach was used to manage the transaction ID.
Flow Configuration
Add the Triton Apex action as the first element
Pass the Interview GUID using
$Flow.InterviewGuid
Configure logging parameters as described in the Flows documentation
Choosing Between Approaches
Direct Parameter Passing
Best for simple, linear execution paths
More explicit and easier to debug
Recommended for initial implementation
Platform Cache
Better for complex scenarios with multiple components
Handles asynchronous operations more elegantly
Useful when direct parameter passing becomes cumbersome
Both approaches will result in the same unified log structure, showing the complete execution path from LWC through Apex and into the Flow.
The Result
Regardless of the approach chosen, you'll see this log structure:
Parent LWC Debug Log
Child Apex Debug Log
Child Flow Debug Log
Additional Flow logs as configured
Any error logs if exceptions occur
This unified view provides complete visibility into your execution path across all three technologies.
Handling Flow Failures
When an unhandled error occurs in your Flow, Pharos automatically captures and correlates this failure with your existing transaction logs. This is possible because:
The Flow's Interview GUID is passed to each Triton log action
Pharos automatically captures unhandled Flow errors
The transaction ID links everything together
Example of an Unhandled Flow Error
Let's modify our example to trigger an error in the Flow:
Resulting Log Structure
When the Flow fails, you'll see this structure in Pharos:
The automatic correlation of unhandled Flow errors is a key feature that helps maintain visibility across your entire execution path, even when things go wrong. This is particularly valuable in production environments where direct Flow debugging isn't available.
Benefits of Automatic Error Correlation
Complete Context: See the entire execution path that led to the failure
No Extra Code: No additional error handling required to capture Flow failures
Production Debugging: Valuable insight into production issues without debug logs
Time Savings: Quickly identify where in the process the failure occurred
For more details on Flow logging and error handling, see the Flows documentation.
Last updated