Monday, 6 September 2021

How to open a record page from finish of flow also close the flow screen ?

 Use case - Navigate to custom object record automatically from screen flow and auto close the flow screen. 

Now a days flow are so advanced we need to leverage flow as much as possible to avoid apex programming. 

Requirement - Create customobject__c record from Lead coping all the values from anotherCustom__c.Both objects are child to Leads. 

There are many ways to achieve to do so. Below is approach I followed to achieve it.

1. Created Before trigger flow to map all the fields  of customobject__c from anotherCustom__c object.

2. Created action in Lead. That action can call lighting component, Visualforce page, and screen flow.Since I'm determined to go via less code, so I leveraged screen flow option. Screen flow contains 2 elements. A. Create Record and B. screen where one aura component loaded.

3. Create Record - This will insert customobject__c record, just mapped only one field Lead Id. Because based on Lead Id, the other flow will make query to pull info from another custom object and then map to custom_object

When Create Record action will be executed then automatically before triggered flow will also be executed so all fields will be mapped. No issue in the data.



Now the next task how to navigate to record page, also need to close the flow screen.

4. Create a aura component which will accept one parameter from flow that is Customobject id then navigate to record page. then close the flow screen.

Here is the component - Flow Redirect 

This can be used to to close any flow and to redirect to any record page. This is a generic component.

Component code - 

<aura:component 
implements="force:appHostable,flexipage:availableForAllPageTypes,
force:hasRecordId,force:lightningQuickAction,
lightning:availableForFlowScreens" access="global" >
    <aura:attribute name="recId" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
</aura:component>

Controller code
({ init : function(component, event, helper) { var newEvent = $A.get("e.force:navigateToSObject"); newEvent.setParams({ "recordId": component.get("v.recId") }); newEvent.fire(); var navigate = component.get("v.navigateFlow"); navigate("FINISH"); } })

Design : This is required so that we can pass parameter from flow
<design:component> <design:attribute name="recId" label="Id Of record" /> </design:component>

Created another aura component - navigateFlow
<aura:component 
implements="lightning:availableForFlowScreens,force:hasRecordId">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>
Controller
({ doInit: function(component, event, helper) { var navigate = component.get("v.navigateFlow"); navigate("FINISH"); } })

5. Added a screen in the screen flow, In that screen added aura component Flow Redirect. also pass custom_object__c id.

Please let me know your feedbacks by adding comments

No comments: