Visual Flows in Salesforce – Finish Behavior

Back to Blog

Share this post

Visual Flows in Salesforce – Finish Behavior

Why should I use Visual Flows?

Enabling dynamically reassignment of a record to different users, creating activities, contacts, or other records just by a click of a button would normally require the development of code in Salesforce. Visual Flow development lets SFDC Admins who are not proficient in Apex coding, to develop anything from small scripts which update the owner of a case, to complicated dynamic wizards which improve user acceptance and automate data entry with the click of a few buttons. Visual Flow in Salesforce is a powerful tool that help developers and admins concentrate on the development part and not have to worry about having to create hundreds test classes for their creations. Overall flows are easily developed with the graphical interface, but some parts can be a little tricky. Below is a little tutorial how to redirect back to the original record after the flow is finished.

Why should I embed my Flow in a Visualforce page?

Under default behavior flows will loop back to the beginning when it finished executing and all global variables will be reset to the default values. This is great for flows that require updating multiple records in a row, but most automated processes will only require the “program” to run once when started. When a flow is embedded in a Visualforce page, the finish behavior can be specified. Preferably we would like to define this finish behavior with as little code as possible, which depending on the desired outcome is not always possible.

The Redirect Tutorial

In this example the Visualforce page will redirect to the original case record on which the button that calls the flow was pressed. This flow reassigns the case to one of three users depending on the case record type.

<apex:page id="thePage" standardcontroller="Case">
	<apex:variable var="theCase" value="{!Case.Id}"></apex:variable>
	<div style="float: left; width: 600px; padding: 10px;">
        	<flow:interview name="Assign_Case_to_designated_Manager" finishLocation="{!URLFOR('/'+theCase)}" >
            		<apex:param name="Case_Id" value="{!Case.Id}"/>
            		<apex:param name="OwnerId" value="{!Case.Owner}"/>
            		<apex:param name="User_Id" value="{!$User.Id}"/>
            		<apex:param name="Case_Record_Type" value="{!Case.RecordType.Name}"/>
            		<apex:param name="Case_current_status" value="{!Case.Status__c}"/>
        	</flow:interview>
    	</div>
</apex:page>

First set the standardcontroller to the object on which the Visualforce page will be called. Remember to add __c when defining custom objects (Custom_Object_Name__c). This will allow you to use query for any field on that active record to use for example as the heading for the page or inside the flow with the apex:param tags.

<apex:page id="thePage" standardcontroller="Case">

Create any apex:variables that are needed later on, since the apex:param variables are passed inside the flow and no longer available in the Visualforce page. In our example we are using ‘theCase’ variable later to set the URLFOR path for the finishLocation of our flow.

<div style="float: left; width: 600px; padding: 10px;">

After designating all variables the flow can be called with the <flow:interview> tag. The name attribute value can be found when navigating to Setup > Create > Workflow and Approvals > Flows > Desired Flow > Unique Name. The finishLocation in this example uses the standard URLFOR tag. ‘/’+theCase will redirect to the current Salesforce environment (for example https://na11.salesforce.com/) plus the value of theCase variable (for example 500G000000ABcDE). The complete resolved URLFOR redirect in our example would then be ‘https://na11.salesforce.com/500G000000ABcDE’.

<flow:interview name="Assign_Case_to_designated_Manager" finishLocation="{!URLFOR('/'+theCase)}" >

Last we define all the <apex:param/> that the flow needs to identify the record and/or conditions. The name needs to be the same as the unique variable name that is defined inside the flow. Do not forget to set the variable Input/Output Type in the flow to “Input only” or “Input and Output”, otherwise the values will not be set by the Visualforce page and the default values will be used instead. The value can be set to a fixed string like “1234 Test” or a value from the record with Object.FieldName (don’t forget to add __c at end of custom objects and custom fields).

<apex:param name="Case_Id" value="{!Case.Id}"/>

The only thing that needs to happen from here is to add the Visualforce page to a custom button and enable it for profiles and desired page layouts. I hope this little tutorial will help setting the finishLocation of a flow without having to write a custom controller. Follow us on Twitter or LinkedIn for updates about new posts and news.

Back to Blog