function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sean M 3Sean M 3 

Visual Flow Lightning Component Redirect

Good morning,

I have created a lightning component which runs a visual flow creating an Event record.

When the flow finishes I would like the page to be redirected to the created Event.

I have found a help topic with some example code but it would be great if someone could explain it ie. where would I add the created record id variable in my flow in this code so that it redirects?

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_using_flow_onfinish.htm
 
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];
         if(outputVar.name === "redirect") {
            var urlEvent = $A.get("e.force:navigateToSObject");
            urlEvent.setParams({
               "recordId": outputVar.value,
               "isredirect": "true"
            });
            urlEvent.fire();
         }
      }
   }
}

Thank you
Best Answer chosen by Sean M 3
Raj VakatiRaj Vakati
Change your code as below
 
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];
         if(outputVar.name === "CreatedCaseId​") {
//The above line to match your flow var
            var urlEvent = $A.get("e.force:navigateToSObject");
            urlEvent.setParams({
               "recordId": outputVar.value,
               "isredirect": "true"
            });
            urlEvent.fire();
         }
      }
   }
}

 

All Answers

Raj VakatiRaj Vakati
Yes .. it will redirect and try once and let me know if any isseu ... 


Make sure you are passing the flow output var from the flow ( in your case records Id as a first output var )
Sean M 3Sean M 3
Ok thanks - in which bit of the code do I add the output var from the flow?
Raj VakatiRaj Vakati
Not in code .. in your flows
Sean M 3Sean M 3
Hi Raj - the variable containing the Event Record ID created in my Flow is called 'CreatedCaseId'. How would I add this to the above code so that it redirects to that page?
Raj VakatiRaj Vakati
Change your code as below
 
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];
         if(outputVar.name === "CreatedCaseId​") {
//The above line to match your flow var
            var urlEvent = $A.get("e.force:navigateToSObject");
            urlEvent.setParams({
               "recordId": outputVar.value,
               "isredirect": "true"
            });
            urlEvent.fire();
         }
      }
   }
}

 
This was selected as the best answer
Sean M 3Sean M 3
Thanks Raj this worked when I reset the app.
AnidevAnidev
Hi Sean and Raj,

Really appreciate the two of you for providing the answers.
However, if you can please post the complete code for both the .cmp and .js file it will be really helpful.

I am using the following, which ain't redirecting

<aura:component access="global" implements="lightning:availableForFlowActions">
    <aura:attribute name="CreatedOppRecord" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    <lightning:flow aura:id="flowData" onstatuschange="{!c.handleStatusChange}" />
</aura:component>

({

init : function (component) {
        // Find the component whose aura:id is "flowData"
        var flow = component.find("flowData");
        // In that component, start your flow. Reference the flow's API Name.
        flow.startFlow("ConvertLeadContactFlow");
    },    
    
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];
         if(outputVar.name === "CreatedOppRecord") {
            var urlEvent = $A.get("e.force:navigateToSObject");
            urlEvent.setParams({
               "recordId": outputVar.value,
               "isredirect": "true"
            });
            urlEvent.fire();
         }
      }
   }
}
})


Thanks in advance.

Regards,
Ani