I donât know about you, but I love streamlining user experiences.  A simple, streamlined process can improve data quality and reduce user frustration.   As we discussed here, Visual Flows are a powerful tool for streamlining and simplifying data entry.   After you build and implement a flow, you only have to enter the data once– and the flow powers the rest of your business process.  But, what if the data already exists somewhere in your Salesforce database?  How can a Flow leverage the data that you already have to create or update other records?  With a little bit of Visualforce, you can extend your flow into an even better tool for managing and maintaining your data.
Letâs say I have a contact record, and I need to create three Orders (a custom object) for items to mail to my contact over the next several months. Â Normally, the mailing address for the order should be the same as the contactâs address, but sometimes I need to use a different address instead. Â A flow will help me create three orders at once with the correct mailing address and shipping dates, and link each order back to the contact. Â Hereâs how.
Building your Flow
When you design your flow, think about the data that you want to feed into it from an existing record and how much flexibility you need. Â Will you ever need to edit the data coming into your flow? Â In my case, I want the primary address from my contact to be the default value for creating my three orders– but I want the ability to change that for the exceptions where it needs to ship to a different address. Â On the other hand, I want the flow to always link the orders created to the contact I started with. Â I donât need an option to change that data within the Flow.
For each piece of data, youâll need to create a variable in your flow– be sure to set the variableâs Input/Output type to âInput onlyâ or âInput/Outputâ so that it can receive values from outside of the flow! Â Each variable needs a unique name. Â Iâve found it helps to name the variable starting with the object that will be the source of the data, just to keep track of where itâs coming from. Â In this case, Iâll create one variable named var_Contact_Street for the street address, and another variable named var_Contact_Id for the contact lookup. Â For the purposes of this example, Iâm only going to use these two variables– but you can create as many as you need to capture your data.
Now that Iâve created my variables, itâs time to put them to work. Â In my Flow, Iâll create a display screen. Â On that screen, I will create a text input field called âStreet Addressâ. Â If I stopped here, the user of the Flow would have to type in the address that already exists on the Contact record. Â This is where the variable comes in! Â Iâll set the default value on the Street Address field to the variable, var_Contact_Street. Â Â Now, the contactâs address will be displayed in the input field– but I have the option to change it if necessary. Â Iâll add one more field to this display screen– a date selector to capture when the first order should ship. Â Weâll call this âShip_Date_1â.
Before I start creating the new records, I need to assign the ship dates for the second and third orders. Â The second order should ship 30 days after the first one, and the third should ship another 30 days after that. Â Â Iâll create two new variables: Â âShip_Date_2â and âShip_Date_3â, and for each of them Iâll set the default value to âShip_Date_1â. Â Then Iâll use the Assignment element within the flow, to add on the extra days. Â For âShip_Date_2â, Iâll set the Operator within the Assignment element to âAddâ, and the value to 30. Â For âShip_Date_3â, Iâll do the same, but Iâll add 60. Â This will update the variables to store dates 30 and 60 days after Ship_Date_1.
Next, itâs time to create my Order records.  I will use the âRecord Createâ element of the flow, and set the object to Order.  When I assign values for the Order to be created, I will set the Contact lookup field to var_Contact_Id.  Even though this value is never displayed to the user, it operates in the background.  For the Street Address, I will select the value of the Street Address screen input field.  Do not use a variables for record creation if you use it  as the default value for a screen input field.  Use the screen input field value instead– it will carry the default value from your variable if the user didnât change it, or the updated value if the user did change it.  Iâll set the ShipDate for this record to use the variable Ship_Date_1.
Now, Iâve created one order. Â I can just copy that element and paste it back into my flow two more times, and just update the ShipDate to Ship_Date_2 on the second and Ship_Date_3 on the third. Â Now, my flow will allow me to create three new Orders, spaced 30 days apart, with one click– think about what this could do for efficiency in your own organization!
Getting Data into the Flow
Now that weâve looked at how to work with the input variables inside the flow, letâs look at how to get your data into them. Â This is where a bit of Visualforce comes in. Â Youâll need to create a new Visualforce page. Â Go to Setup > Develop > Pages, and click âNewâ. Â A basic page with some default content will appear. Â You can delete the default content– just keep the <apex:page> and </apex:page> tags.
Youâll also need to set the standard controller– thatâs telling the page what object youâre starting from. Â In this case, since Iâm pulling data from a Contact record, I want to use the Contact standard controller. Â So, Iâll edit my <apex:page> tag as follows:
<apex:page standardController=âContactâ>
If you want to pull data starting from a different object, such as an Opportunity or an Account, you would enter that instead of âContactâ when declaring the standard controller for the page.
Now, you just need a couple more lines of code. Â The first thing you need is a tag for the flow: Â <flow: interview name=âFlowNameâ> (replace FlowName with the name of your Flow). Â Youâll also need the closing tag: </flow:interview>. Â These couple of lines of code tell your page to launch the flow. Â Now, we just need to get the data into it.
To get data into our Flow using Visualforce, weâll use the <apex:param> tag. Â For each variable, you will declare the variableâs name in the flow, and its value from the object, using the following syntax:
<apex:param name=âvar_Contact_Idâ value=â{!contact.Id}â/>
The name is always the flow variable, and the value is always the API name for the data from the record.
So, your finished page should look something like this. Â Iâve bolded the values that might be different for you.
<apex:page standardController=”Contact“>
<flow:interview name=”OrderFlow“>
<apex:param name=”var_Contact_Id” value=”{!contact.Id}“/>
<apex:param name=”var_Contact_Street” value=”{!contact.MailingStreet}“/>
</flow:interview>
</apex:page>
Now, your page is ready to go. Â Click Save and move on to the next step!
Put a button on it
Now that we have our flow and our page ready, letâs put it all together with a custom button. Â Create a custom button on Contacts (or whatever object youâre working with), and set the content source to âVisualforce Pageâ. Â Select the page you just created from the âContentâ menu and click âSave.â Â Place your new button on a page layout, and youâre ready to go!
Now you know how to use Flows and a bit of Visualforce to leverage the data that youâre already managing in Salesforce. Â Stay tuned for my next blog, where Iâll explore a few more advanced applications of Flows!