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
RajaramRajaram 

Dynamic redirect from a VF page with an embedded Flow

I just found out the way you can implement dynamic redirect with Flows embedded in a VF page. Here is an example of the VF page and the controller which redirect the user to the detail page of a Lead created within the Flow.

 

VF Page:

<apex:page controller="myAutoInsuranceController" sidebar="false" showHeader="false" >
<flow:interview name="Auto_Insurance_Quote" interview="{!myAutoFlow}" finishLocation="{!nextPage}"/>
</apex:page>

 

Controller:

public class myAutoInsuranceController {

public Flow.Interview.Auto_Insurance_Quote myAutoFlow { get; set; }

public String getmyID() {
if (myAutoFlow==null) return '';
else return myAutoFlow.vaLeadID;
}

public PageReference getNextPage(){
PageReference p = new PageReference('/' + getmyID() );
p.setRedirect(true);
return p;
}

}

HongjunHongjun

Raj, when I try this code, myAutoFlow is always null. Do I need to call setter somewhere?

RajaramRajaram

Well you have to change the code to launch your flow and access your variables

 

public Flow.Interview.Auto_Insurance_Quote myAutoFlow

 

myAutoFLow here would be an instance of a flow whose unique name is "Auto_Insutance_Quote" and this has to match the flow:interview property.

 

 

can you copy and paste your version of the VF page and controller?

HongjunHongjun

VF page:

<apex:page standardController="Case" extensions="CaseFlowExtension" tabStyle="Case" >
  <flow:interview name="new_flow" interview="{!myAutoFlow}" finishLocation="/{!nextPage}" >
    <apex:param name="caseId" value="{!case.CaseNumber}"/>
  </flow:interview>
</apex:page>

 

Controller:

public with sharing class CaseFlowExtension {
  private final Case cas;
  public Flow.Interview.new_flow myAutoFlow { get; set; }
   
  public CaseLoanFlowExtension(ApexPages.StandardController stdController) {
        this.cas = (Case)stdController.getRecord();
    }

    public String getMyFlowVar() {
        if (myAutoFlow==null) return '';
      else return myAutoFlow.vaFlow;
    }
    public String getNextPage() {
        String vaFlow = getMyFlowVar();
        
        if (vaFlow.equals('')) { return cas.Id;}

        else if (vaFlow.equals('stop')) { return cas.Id; }
        return null;
    }
}

 

The controller is extension of Case standard controller. What I am trying to do is to call my Flow after save a Case. But myAutoFlow is always null in the getMyFlowVar(). I can't get the value from variable vaFlow which is defined in my Flow. Any idea what is wrong? Do I need to initiate myAutoFlow?

RajaramRajaram

Not sure what you mean by "calling flow" aftet the case is saved. When and how is the VF page being invoked?

HongjunHongjun

Sorry I forgot to paste save() method. This is handling saving a Case. It will return the Flow page reference.

public PageReference save() {
      try {
        upsert(cas);
      } catch(System.DMLException e) {
        ApexPages.addMessages(e);
        return null;
      }

      //Case edit view
      PageReference ref = (new ApexPages.StandardController(cas)).view();
      
      if (cas.Reason.equalsIgnoreCase('Flow request')) {
          ref = Page.NewFlowPage;
          ref.setRedirect(false);
      }
      return ref;
    }

jordanmjordanm

This is very useful. I have applied it to my scenario, but cannot figure out how to write a test case for it.

How would you fill the flow variable name? 

tkronzaktkronzak

Hey Rajaram,

 

I've implemented the Class and VF page identically (minus the sidebar/header=False notations). It runs fine until it's time to load NextPage -- Instead of bringing me to my instance with a /RecordID, it wants to completely re-load the Salesforce login page.  I'm running it in a Sandbox FWIW, but I can't imagine that makes any difference can it? Any suggestions?

Morgan@crocsMorgan@crocs

Does anyone have a test method for the original post. I can only seem to get 12% coverage

tkronzaktkronzak

Figured it out - The Visual Workflow needed to proceed to the next "step" to properly assign the ID to the variable within the workflow after the record is created.

 

I also have really low code coverage as well.

annoyingmouseannoyingmouse

This is brilliant, thank you ever so!

goabhigogoabhigo

I have just implemented something similar. Wondering how you wrote test class for this. Can you please share some inuts/code on the test class for myAutoInsuranceController ??