A reusable Redirect controller for VisualFlows

The problem at hand

Visualflow is one of the most powerful tools available to Salesforce Admins and Developers. Often the biggest barrier to adoption isn’t a technnowGoFindItical issue of capabilities but the lack of realization that visualflows can do that! Unfortunately, one of the technical issues that seems to come up often (at least recently) is how to create a record in a flow, and then upon successful completion of the flow, redirect the user to the new record. The use cases are pretty broad, but I was roped into the following use case. A flow is written to guide users through creating a case. When the case is created, and the flow is finished we want to redirect the users to the newly created case’s detail page. Sounds simple right?

Good Guy VisualFlow.

Unfortunately, the finishLocation=”” attribute of the Visualforce flow tag doesn’t accept flow variables. It’s therefore impossible, at this time, to create a flow with a programmatically defined finishLocation. What you can do, however, is generate a Visualforce controller that utilizes a getter function to programmatically generate the finishLocation attribute. Rather than creating these controllers one-off as you need them, I’ve created a reusable Visualforce Controller, that you can utilize with any flow you write, to redirect to any given RecordID.

Show Me The Code.

Note well You need to create a flow named “RedirectFlow” that consists of a decision step that launches the flow you actually want to kick off. Line 4 of the visualforce page is a parameter for defining which flow you actually want to start. This “wrapper flow” bit is needed to make the controller re-usable. Big thanks to SalesforceWizard  for pointing out the mistake I made. He’s the man.

<apex:page Controller="FlowRedirectController">
<flow:interview name="RedirectFlow" interview="{!redirectTo}" finishLocation="{!finishLocation}" >
<!– This Parameter is *required!* –>
<apex:param name="StartFlow" value="YOUR_FLOW_NAME_HERE" />
Any Params you need to pass into your flow.
<apex:param name="CaseId" value="{!CaseId}"/>

Public class FlowRedirectController{
Public Flow.Interview.RedirectFlow redirectTo {get; set;}
public FlowRedirectController() {
Map<String, Object> forTestingPurposes = new Map<String, Object>();
redirectTo = new Flow.Interview.RedirectFlow(forTestingPurposes);
Public PageReference getFinishLocation(){
String finishLocation;
if(redirectTo != null) {
finishLocation = (string) redirectTo.getVariableValue('vFinishLocation');
PageReference send = new PageReference('/' + finishLocation);
return send;

private class Test_FlowRedirectController{
static testmethod void SetVariablesTests() {
PageReference pageRef = Page.ExampleVFFlow;
FlowRedirectController testCtrl = new FlowRedirectController();
System.assertEquals(testCtrl.getFinishLocation().getUrl(), '/codefriar.wordpress.com/');


  1. Can you let me know what the param name is defining in your VF page and where you would pull it from your flow?

    I was getting an error saying the param name is not in my flow.


Leave a Reply

Your email address will not be published. Required fields are marked *