Elevating Salesforce User Experience Aura Component for Redirecting to Records from Screen Flows

Elevating Salesforce User Experience: Aura Component for Redirecting to Records from Screen Flows

,

Introduction

Hi Everyone, Welcome to our new blog post – “Custom Aura Component for Redirecting to Records From Flows”.

In the ever-evolving landscape of Salesforce development, enhancing user experience is a top priority. One common challenge has been the absence of a direct method to redirect users to specific records after executing a screen flow.

In this blog post, we’ll explore a powerful solution using an Aura component, tailored for Salesforce developers and admins, which can be easily configured and used inside screen flows to redirect users to records.

Versatility: Console App and Non-Console App Compatibility

A standout feature of our solution lies in its compatibility with both console apps and non-console apps.The closeTab function utilizes the Lightning Workspace API to intelligently handle navigation, ensuring a seamless experience for users in diverse Salesforce environments.

Code Explanation:

Here is the full Aura Component Code which can be used as an action in screen flow for Redirecting to Records From Flows and its explanation –

1. Component Definition (RedirectToRecord.cmp):
<aura:component implements="force:lightningQuickAction,lightning:availableForFlowActions">
    <aura:attribute name="recordId" type="String" access="public"/>
    <aura:attribute name="parentRecordId" type="String" access="public"/>
    <aura:attribute name="childObjectName" type="String" access="public"/>
    <aura:attribute name="parentObjectName" type="String" access="public"/>
    <lightning:workspaceAPI aura:id="workspace"/>
</aura:component>

The component defines several attributes and includes the lightning:workspaceAPI component.

  • Attributes:
    1. recordId: Represents the ID of the record to which the component will redirect.
    2. parentRecordId: Represents the ID of the parent record (in case of console app)
    3. childObjectName: Represents the name of the child object.
    4. parentObjectName: Represents the name of the parent object.
  • Dependencies:
    1. force:lightningQuickAction and lightning:availableForFlowActions: These interfaces make the component available as a Quick Action and for Flow Actions, respectively.
    2. lightning:workspaceAPI: Used to interact with the workspace, allowing the component to handle tab navigation.
2. Design Definition (RedirectToRecord.design):
<design:component>
    <design:attribute name="recordId" label="Record ID" />
    <design:attribute name="parentRecordId" label="Parent Record ID" />
    <design:attribute name="childObjectName" label="Child Object Name" />
    <design:attribute name="parentObjectName" label="Parent Object Name" />
</design:component>

This component defines the below design attributes for the component, allowing administrators / developers to set values when adding the component to the flow to redirect to the record.

  • recordId: Represents the ID of the record to which the component will redirect.
  • parentRecordId: Represents the ID of the parent record (in case of console app)
  • childObjectName: Represents the name of the child object.
  • parentObjectName: Represents the name of the parent object.
3. Controller (RedirectToRecordController.js):
({    
    invoke : function(component, event, helper) {        
        helper.closeTab(component, event, helper);
    }
})

The controller has a single method, invoke, which calls the closeTab method from the helper. This separation of concerns follows best practices in Lightning component development.

4. Helper (RedirectToRecordHelper.js):
({
    closeTab: function (component, event, helper) {
        // Retrieve attribute values
        var record = component.get("v.recordId");
        var parentRecordId = component.get("v.parentRecordId")
        var childObjectName = component.get("v.childObjectName");
        var parentObjectName = component.get("v.parentObjectName")

        // Access the Lightning workspace API
        var workspaceAPI = component.find("workspace");

        // Check if the workspaceAPI is defined
        if (workspaceAPI !== undefined) {
            // Check if the user is in the Salesforce Console
            workspaceAPI.isConsoleNavigation().then(function (response) {
                // If in the Console, open a new tab for the parent record
                workspaceAPI.openTab({
                    url: '/lightning/r/' + parentObjectName + '/' + parentRecordId + '/view',
                    focus: true
                }).then(function (response) {
                    // Open a subtab for the child record under the parent tab
                    workspaceAPI.openSubtab({
                        parentTabId: response,
                        url: '/lightning/r/' + childObjectName + '/' + record + '/view',
                        focus: true
                    });
                })
                .catch(function (error) {
                    // If there's an error, log it, and fall back to standard navigation
                    console.log(error);
                    var redirect = $A.get("e.force:navigateToSObject");
                    redirect.setParams({
                        "recordId": record
                    });
                    redirect.fire();
                    $A.get('e.force:refreshView').fire();
                });
            })
            .catch(function (error) {
                // If there's an error checking for Console navigation, log it, and fall back to standard navigation
                console.log(error);
                var redirect = $A.get("e.force:navigateToSObject");
                redirect.setParams({
                    "recordId": record
                });
                redirect.fire();
                $A.get('e.force:refreshView').fire();
            });
        }
    },
})

The Lightning Workspace API is designed to intelligently use for console navigation and gracefully handle any errors in this helper method.If the user is not in the console, it falls back to standard navigation using the force:navigateToSObject event. After navigation, this helper method triggers a view refresh to ensure it displays the latest data.

Sample Scenario: Creating a Case from an Account

Let’s consider a scenario. You’ve implemented a screen flow that allows users to quickly create a case from an account. After the case is created, you want the user to seamlessly transition to the newly generated case record for further actions.

CreateCase1
CreateCase2

Demo:

Other ways to redirect –

https://salesforce-flowsome.com/how-to-redirect-users-returl/

Note – Workspace API is currently in beta in LWC, once it become generally available, will create a new blog post for it - https://help.salesforce.com/s/articleView?id=release-notes.rn_lwc_workspaceAPI.htm&release=246&type=5

For more helpful articles please visit – https://thesalesforcedev.wordpress.com

2 responses to “Elevating Salesforce User Experience: Aura Component for Redirecting to Records from Screen Flows”

  1. Great post on enhancing Salesforce user experience! The custom Aura component for redirecting users to records from screen flows is a game-changer. I appreciate the detailed breakdown of the component code, design, controller, and helper functions. Can you share more real-world scenarios where this Aura component significantly improved user navigation and productivity within Salesforce? Looking forward to your next insightful blog on Salesforce development tips!

    Like

  2. […] List Using Custom Button. This in continuation to our previous blog post, where in we showcased a aura component to redirect to record from a flow, now we will be showing the same using LWC […]

    Like

Leave a comment