Last Updated on June 12, 2022 by Rakesh Gupta
Have you ever come across a situation in Flow where you wanted to redirect your users to a newly created record but did not know how to handle/configure/implement it?
Yep, pretty frustrating memory – who could forget? Well, thanks for reading my blogs, you’re in luck! Using different use cases, I have written around a hundred articles on Salesforce Flow to guide users on how to use Flow Builder. You just landed at the right place to learn about it.
This Solution will only work in Lightning Experience.
By default, when a flow finishes, (1) a new interview starts and users see the first screen of the flow; or (2) you can redirect users either to an old record or to the record from where s/he started the flow.
In this article, I am going to show you how, you can create a Lightning component, that calls Flow Local Action to redirect users to newly created record.
Before we move ahead, let us understand what is a ‘Flow Local Action’ and, its power.
When you use Lightning components as flow actions, you can execute actions in the client-side (browser) rather than going through the Salesforce server. Like Lightning Components, Flow Local Actions executes on your device in the browser. For example, you can perform the following actions:
- Open a newly created record in another browser tab
- Open a list view
- Display pop-up alert
- Play Sound (For example, when an opportunity is closed-won)
- Use the Camera of the laptop/workstation
- Reload the web page
- Open a URL in another window or tab
- Get the data from a third-party server without going to the Salesforce server etc.…
Once you configure the Lightning component markup; (i) client-side controller; and, (ii) design resource, appear in Flow Builder as a Local Action.
Today, in this article, I am going to show you how you can create a Lightning component that calls Flow Local Action to redirect the user to the newly created lead record.
Business Use Case
Edward Backhouse is working as a System administrator at GurukulOnCloud. Edward received the following requirement from his manager – create a lead capture form which looks like the following screenshot:
Once a user clicks on the Finish button, redirect them to the newly created lead record. Later, Edward has to put this form on the Home Page Layout and open it for the entire world.
Pre-requests
Please go through these articles first:
- Getting Started with Salesforce Flow Designer – Part 2 (Do You Know How Screen Element Bestows Power on Flow Builder?!)
- Create and Edit Aura Components (Source Trailhead)
A solution for the above business requirement
To solve this requirement, we will use many ingredients – including, Salesforce Flow, Lightning Component, and a Flow Local Action.
Let’s take the discussion further and understand how you can develop this solution end-to-end. Perform the following steps to create the solution using the new Flow Builder:
- Log-in into your developer org.
- Click on Setup (Gear icon) | Developer Console.
- To Create a new lightning component, click on File | New Lightning Component, and configure it – as shown in the following screenshot:
- Now copy paste the following code – feel free to change it based on your business requirement.
-
Component Markup <aura:component implements="lightning:availableForFlowActions" access="global" > <aura:attribute name="leadId" type="String" access="global"/> </aura:component>
-
Client-side Controller ({ invoke : function(component, event, helper) { var redirectToNewRecord = $A.get( "e.force:navigateToSObject" ); redirectToNewRecord.setParams({ "recordId": component.get( "v.leadId" ), "slideDevName": "detail" }); redirectToNewRecord.fire(); } })
-
Design Resource <design:component> <design:attribute name="leadId" label="Id of the New Record" /> </design:component>
-
- Things to understand before proceeding to the next step:
- lightning:availableForFlowActions: Implement lightning:availableForFlowActions interface to make sure that lightning component is available as flow local actions.
- When the local action is executed, the flow calls the invoke method, which uses the force:navigateToSObject event to navigate to the created record. The code runs asynchronously in your client-side controller
- If you want to make an attribute’s value customizable in Flow Designer, add it to the component’s design resource. That way, flow creator can pass values between that attribute and the flow when s/he configures the Core Action element. The leadId attribute is declared in the design resource so that it is configurable in the local action.
- The next step is to create a flow that uses the above lightning component. To do so, navigate to Setup (Gear Icon) | Setup | Process Automation | Flows.
- Now click on the New Flow button; it will open Flow Builder for you.
- Next, we will use the Screen element to create a lead capture form. Drag and drop a Screen element onto Flow Builder and configure it – as shown in the following screenshot:
- The next step is to insert the data entered by the user, in the lead capture form, to Salesforce. For this, we will use a Create Records element. Drag and drop a Create Records element onto Flow Builder and configure it – as shown in the following screenshot:
- Now we will use our next flow element i.e. core action (NavigateToNewRecord) which we created in step 4. This local action takes the user to a lead record page in the Lightning Experience. Drag and drop an Action element onto Flow Builder and configure it – as shown in the following video:
- In the end, Edward’s Flow will look like the following screenshot:
- Once you’re done, Save the flow and name it NavigateToNewRecord, with Type: Screen Flow. Do not forget to activate the Flow by clicking on the Activate button.
- The final step is to add the NavigateToNewRecord to the Lightning Experience Home page. Follow the instruction as shown in the following video to add a Flow to Lightning Experience home page:
- Once you are done, click on the Save button.
It’s time to test the Flow!
Now, if a User creates a record using flow component that we added to the Lightning Experience Home page, they will redirect to new lead record as soon as they click on the finish button.
–> If you like this article, or would like to share some ideas for enhancements then, please do so. I will very much appreciate it. Thanks in advance.
Proofreader: - Munira Majmundar
Is it possible to redirect to the record in edit mode instead of view?
I also having been trying to accomplish this as a list view button, not from the home screen. Any suggestions on how to make that work – from the list view page, it just cycles back to the beginning of the screen flow. Thank you!
Can you share your Flow configuration with me through email? I look forward to hearing from you soon.
Tested this solution by calling a flow from a list view button – the redirect is not working.
Is the issue that when invoking a flow from a button, does this redirect work?
I edited your version to work for Opportunities.
When finishing the flow, it returns me to the start of the flow screen and not the newly created Opportunity Record.
Send me your code and screenshots. I will take a look
I have the same fate as Steve. It stated “Page does not exist. Enter a valid URL and try again”. I believe the problem lies in the Lightning Component. I do not know how to code and where to make the change 😟
Can you please send me complete steps with screenshots via email at contact@automationchampion.com
Hello Jeffrey, you might not have given the new Lead Id in the Action window. If you provide the ID in the “Set Input Values” text field, your issue will be resolved.
Thanks, Rakesh.
What is the solution if we are in classic?
This doesn’t work for me. It just says page doesn’t exist and enter a valid URL. Also since it’s only one screen to this flow. I only have a next button and not a finish button.
I followed your exact steps but when I went to select the core action in the flow it was not listed. Why? I have never developed before am I missing a step where I need to activate the local action from the developer console? All I did was save it.
Nevermind. I closed my browser and re opened it and it was there
This was very helpful and easy to follow these steps, thank you! Is it possibly to redirect to newly created record & have it open in edit mode?
Yes, but you have to modify the Lightning Component a bit.
Brilliant!! Is it possible to use this when you’re lauching the flow from an action instead of from a lightning component.
I am sure you can, but never tried.
Thank you Rakesh! This worked perfectly on the first round! Very useful in so many ways.
Thanks for the feedback, Nick! I am glad this was useful!
Hi Rakesh, this is the error I getting after adding this flow component to the home page
“We can’t launch this flow because of a variable error. Send this error message to your admin. The value null is being provided for variable VarTEmailAddress but isn’t compatible with the variable’s data type (String). Check the places where you implemented the flow ‒ such as through a Lightning page or a custom button ‒ and make sure that all provided values are compatible with the variables’ data types.”
What could be wrong?
Why not use: https://developer.salesforce.com/docs/atlas.en-us.salesforce_vpm_guide.meta/salesforce_vpm_guide/vpm_url_set_retURL.htm
Because of this will not work with newly created record.