+ Start a Discussion
Akis AthanasiadisAkis Athanasiadis 

Lightning component redirect to record

I have created a flow and a lightning component which run on new.
The flow creates a record and I want at the end of the flow to be redicted inside the record which was  just created.
In the flow I store the new record's id as {!recordId}

The controller is:
 
({
    init : function (component) {
        // Find the component whose aura:id is "flowId"
        var flow = component.find("flowId");
        // In that component, start your flow. Reference the flow's Unique Name.
        flow.startFlow("Internal_Requests");
        
    },
handleStatusChange : function (component, event) {
    
    if(event.getParam("status") === "FINISHED") {
       var navEvt = $A.get("e.force:navigateToSObject");
            navEvt.setParams({
                "recordId": "??????????",
                "slideDevName": "related"
            });
            navEvt.fire();
         }
      },
})


What do i set at the record Id though to be redicted in the new record?

All the threads I have found, provide specific Id which of course does not apply to our needs.

Best Answer chosen by Akis Athanasiadis
Nayana KNayana K

I am sorry. Looks like you are overriding 'New' button. So, definitely recordId will be not be present.

I am assuming you are talking about flow output variable whose name is 'recordId'.
 
handleStatusChange : function (component, event) {
    
    if(event.getParam("status") === "FINISHED") {
       var outputVariables = event.getParam("outputVariables"); 
       var outputVar; 
       for(var i = 0; i < outputVariables.length; i++) { 
             outputVar = outputVariables[i]; // Pass the values to the component's attributes 
             if(outputVar.name === "recordId") { 
                  var navEvt = $A.get("e.force:navigateToSObject"); 
                  navEvt.setParams({ "recordId": outputVar.value, "slideDevName": "related" }); 
                    navEvt.fire(); 
              } 
        } 
    }
},

 

All Answers

Nayana KNayana K
if(event.getParam("status") === "FINISHED") {
         // Get the output variables and iterate over them
         var outputVariables = event.getParam("outputVariables");
         var outputVar;
         for(var i = 0; i < outputVariables.length; i++) {
            outputVar = outputVariables[i];
            // Pass the values to the component's attributes
            if(outputVar.name === "recordId") {
              var navEvt = $A.get("e.force:navigateToSObject"); navEvt.setParams({ "recordId": outputVar.value,          "slideDevName": "related" }); navEvt.fire(); }
            } 
         }
      }

if recordId is output variable, above may work. 

I referred this documentation and edited the code:https://developer.salesforce.com/docs/atlas.en-us.216.0.salesforce_vpm_guide.meta/salesforce_vpm_guide/components_using_flow_inputs_get.htm
Akis AthanasiadisAkis Athanasiadis
I have tried to to 
"recordId": "{!recordId}"

but it does not work unfortunately. is there something I am missing here?

This is the cmp
 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,lightning:actionOverride,lightning:availableForFlowActions" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    <lightning:flow aura:id="flowId" onstatuschange="{!c.handleStatusChange}" />
</aura:component>

 
Nayana KNayana K
Do you want recordId of lightning page? I can see you have included flexipage:availableForRecordHome,force:hasRecordId interfaces. 

If yes, 
then 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,lightning:actionOverride,lightning:availableForFlowActions" access="global" > 

<aura:attribute name="recordId" type="String" />
<aura:handler name="init" value="{!this}" action="{!c.init}" /> <lightning:flow aura:id="flowId" onstatuschange="{!c.handleStatusChange}" /> </aura:component>


 
({
    init : function (component) {
        // Find the component whose aura:id is "flowId"
        var flow = component.find("flowId");
        // In that component, start your flow. Reference the flow's Unique Name.
        flow.startFlow("Internal_Requests");
        
    },
handleStatusChange : function (component, event) {
    
    if(event.getParam("status") === "FINISHED") {
       var navEvt = $A.get("e.force:navigateToSObject");
            navEvt.setParams({
                "recordId": component.get("v.recordId"),
                "slideDevName": "related"
            });
            navEvt.fire();
         }
      },
})


 
Nayana KNayana K
If it is flow's output variable, then check my first answer.
 
Nayana KNayana K

I am sorry. Looks like you are overriding 'New' button. So, definitely recordId will be not be present.

I am assuming you are talking about flow output variable whose name is 'recordId'.
 
handleStatusChange : function (component, event) {
    
    if(event.getParam("status") === "FINISHED") {
       var outputVariables = event.getParam("outputVariables"); 
       var outputVar; 
       for(var i = 0; i < outputVariables.length; i++) { 
             outputVar = outputVariables[i]; // Pass the values to the component's attributes 
             if(outputVar.name === "recordId") { 
                  var navEvt = $A.get("e.force:navigateToSObject"); 
                  navEvt.setParams({ "recordId": outputVar.value, "slideDevName": "related" }); 
                    navEvt.fire(); 
              } 
        } 
    }
},

 
This was selected as the best answer
Akis AthanasiadisAkis Athanasiadis
Nayana,
I put your code for the component and I replaced the part that i needed to in the controller but I get error:
 
Uncaught Action failed: c:Supplies$controller$handleStatusChange [Cannot read property 'length' of undefined]
Callback failed: serviceComponent://ui.interaction.runtime.components.controllers.FlowRuntimeController/ACTION$execu

 
Nayana KNayana K
Looks like outputVariables.length is the line where you getting error.  Which means outputVariables is undefined. Are you sure recordId is set as output variable..put console debugs and check 
Akis AthanasiadisAkis Athanasiadis
aaaaaaaah.... i had it set in the create record but I hadn't set the recordId as an output use.
I did it and it is working now :D
Thank you very very much :D