Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Navigating to a Record from a Screen Flow? Here’s a Simple LWC Workaround!

Ever found yourself in a Salesforce Screen Flow, wishing there was an easy way to redirect users to a specific record once the flow finishes? Unfortunately, Salesforce doesn’t provide an out-of-the-box (OOTB) solution for this seemingly simple task. Screen Flows are powerful tools for guiding users through processes, but when it comes to automatically navigating to a record (say, after creating or updating it), you’re left to find workarounds.

That’s where a custom Lightning Web Component (LWC) swoops in to save the day! In this post, we’ll build a lightweight LWC that not only navigates to a record as soon as it loads but also signals the flow to gracefully finish using the FlowNavigationFinishEvent. Let’s dive in!

The Problem: No Native Redirect After a Screen Flow

Screen Flows are excellent for automating business processes, but Salesforce doesn’t offer a native way to automatically redirect users to a specific record once the flow finishes. Imagine you’re creating a new record using a Screen Flow and want the user to land directly on the record view page without clicking any additional buttons. Currently, the flow would just end, leaving the user to navigate manually—a less-than-ideal experience.

To address this gap, we can leverage an LWC in combination with Salesforce’s NavigationMixin to automatically perform the redirection. We’ll also use the FlowNavigationFinishEvent to tell the flow to end as soon as the redirection occurs. Think of it as a smooth handoff between the flow and the record page!

The Solution: Building the Navigate-to-Record LWC

Here’s a step-by-step breakdown of how to create the LWC:

1. HTML File

Since this component’s primary job is navigation, there’s no UI required. The HTML is empty:

html

<template></template>

2. JavaScript File

This is where the magic happens! The component uses the NavigationMixin to redirect users to the desired record and dispatches the FlowNavigationFinishEvent to close the flow.

javascript

import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { FlowNavigationFinishEvent } from 'lightning/flowSupport';

export default class NavigateToRecordFlow extends NavigationMixin(LightningElement) {
    @api recordId; // Input property from Screen Flow

    connectedCallback() {
        // Navigate to the record view
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                recordId: this.recordId,
                actionName: 'view'
            }
        });

        // Signal the flow to finish
        const finishEvent = new FlowNavigationFinishEvent();
        this.dispatchEvent(finishEvent);
    }
}

3. Metadata File (.js-meta.xml)

To make the LWC available in Screen Flows, the metadata file must define the lightning__FlowScreen target and the recordId property.

xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>57.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__FlowScreen</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__FlowScreen">
            <property name="recordId" type="String" />
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

How It Works

  • The recordId is passed to the LWC as an input from the Screen Flow.
  • When the component loads (connectedCallback()), it triggers a navigation action to the record page using NavigationMixin.
  • After the redirection, the FlowNavigationFinishEvent tells the flow to terminate seamlessly.

Why Is This a Good Workaround?

While Salesforce doesn’t have an OOTB way to achieve this, this LWC workaround is both simple and effective. It doesn’t clutter the flow with unnecessary steps or require additional clicks from users. The navigation and flow finishing happen automatically, providing a smoother experience.

Implementation Steps

  1. Save the code files in your Salesforce project folder.
  2. Deploy the LWC to your Salesforce org.
  3. Use the LWC in a Screen Flow, passing the recordId as an input variable.
  4. Test it out—watch as the flow redirects you to the record page and finishes effortlessly!

Final Thoughts

With just a few lines of code, you can transform the user experience in your Screen Flows. This simple LWC makes it easy to navigate to a record and elegantly conclude the flow without any extra manual effort. While Salesforce might not have an OOTB solution for this yet, it’s amazing what a small custom workaround can achieve.

Give it a try and let me know how it works for you! Happy building! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *