April 16, 2015
A subflow is nothing tricky, it’s just a flow that is called by another flow. And fortunately calling one flow from another is refreshingly easy to do when you keep just a few things in mind.
But first of all, why would you want to call a subflow? One reason is to write your own functions that can be used over and over again. For example, one useful function looks up the RecordTypeId for an object you are referencing in your primary flow. Another example would be a function to concatenate strings with an optional separator (examples of these subflows to come).
Another reason is to be able to run a section of code from two or more other processes. For example, let’s say I have a little flow that adds a record to a custom object B. Just before adding the record it populates a couple fields with calculated data so that these fields are always predictably populated.
In this example the functionality of that code needs to be available to the user on object B itself, and also to a flow from related object A. In order to make this happen I will call the subflow from a “wrapper” flow on object B, and from the flow that is initiated from object A (more to come on wrapper flows).
So what do you need to keep in mind when calling a subflow? Here are just a few observations:
Subflows Are Gift Exchange Addicts
The first thing to know is that subflows likes to receive input and give output. They don’t just like it, the require it and if you don’t participate, they won’t play with you.
This means that you must have at least one variable defined in your subflow to receive input and to give output, even if input and output is not needed for the functionality of the subflow or primary flow.
Variables in your subflow that receive or give out data must be defined as input or output variables–“Input Only” if it will be receiving data only, “Input and Output” if it will be both receiving and giving data, or “Output Only” if it will be giving out data only. So in your subflow you’ll need one variable that is “Input and Output,” or two variables, one that is “Input Only” and one that is “Output Only.”
That means that you’ll need to pass something from your primary flow into these variables in your subflow, and you can pass in all sorts of things, like screen input fields, variables, constants, typed strings or numbers, and even functions.
A Word of Caution About Changing Subflow Input or Output Variable Names
If you should happen to change the name of an Input or Output variable in your Subflow you will need to go into all the Flows that call your Subflow and re-select the Source variable. For example, if your original input variable is “tempVar” and you rename it to “inputVar”, then you’ll need to go into all the calling flows, open the Subflow element, and in the Source field, where you see “tempVar” change it to “inputVar”.
Accessing Newly Created Subflow I/O Variables
If you happen to be working on both your primary flow and your subflow simultaneously, like I somehow seem to be doing quite frequently, you may make a change to your subflow I/O variables but not be able to see those variables in the subflow element in your primary flow. To correct this, first be sure you’ve saved both your subflow and your primary flow. Then, on the browser page for your primary flow, click the browser refresh button. This will load the changed you made into the subflow elements and you’ll be able to see your new or changed I/O variables.
Quirky Save Message
There are a few quirky items to be aware of when working with flows, but here is just one for this post: you’ll always get a general warning message when saving a flow that passes one or more sObject Collection variables.
This message startled me at first, so I very carefully went back over all of my variables to make sure their types matched exactly, and found that everything looked just fine. So I barged ahead with testing and everything worked splendidly. In my observations this seems to only occur when passing sObject Collection variables. Passing singular sObject variables doesn’t trigger this warning.
Posts with Subflow Examples:
- Here is a link to a very good article written by Joe Morse on creating and calling a subflow.
- This is a link to the Salesforce help documentation for adding a Subflow.