Tuesday, July 1, 2014

Think Like a Developer When You Design Flows

Flows offer a lot of power to clicks-not-code administrators who want to automate processes using record lookup, update and create options offered with this easy-to-use feature.  Of course, Flows do require that administrators learn to think like developers a bit.  They must be able to outline the process the Flow should take, including decision points where the flow might go in one of multiple directions.  They must also think of ways to test and debug their Flows and to create Flows that can be reused or called in multiple ways.
You need to know how to look for bugs in your Flow.

Test Extremes

One rule of testing is to keep in mind the extremes and the improbable.  What happens when users enter data that is out of scope, or when a record that the Flow expected to exist (like a generic, default Account record) has incorrectly been deleted.  Make sure your Flow includes fail-safe procedures and uses error messages, which Flow makes available, to catch these situations.

Break Points and System.Debug

As you are developing your Flow, be liberal with Screens to display the values of variables along the different Flow paths.  One of the things I love most about Flows is the ability to create Screens that I do not tie into the Flow.  This enables me to create break points that halt the Flow before the error occurs and lets me display debug messages for myself between steps, providing the values of variables needed in subsequent steps.  When the Flow is ready to be activated for users, I can first disconnect all of the extra Screens and leave them in the Flow if there is a chance I will need to rework the Flow.

For example, between the record lookup step that assigns data to variables and the decision branch step where the Flow's path will be determined according to variable data, I will create a Screen to display the record ID and all of its variable data.  I won't bother to connect the Screen unless I find that the decision branch does not behave the way I expected.  I can even leave the Screen in place to use as in future versions of the Flow or to use later, when the data model defined with Salesforce objects and fields may have changed (the 'model' part of the Flow).  Otherwise, the Screen stays disconnected from the Flow.

Flow Triggers

For Flows to be used with Workflow rules, they are expected to operate without a user and so trigger-ready Flows cannot have Screens, which require a Flow user to click the 'Next' button to proceed.  This can complicate the debugging process, but don't be discouraged.  When you are first creating a complicated flow, you can still use Screens and simply call the Flow from a link or, to easily pass parameters, a Visualforce page.  See my earlier post for more information about calling a Flow from Visualforce.  Keep in mind that trigger-ready Flows cannot include Screens with error messages and so should be thoroughly tested outside of the workflow rule, to work out ways to avoid errors during the Flow execution.

Deleting the leftmost and rightmost columns
makes this Flow trigger-ready without Screens

Design Patterns

Converting a working Flow that has been created with Screens to one that is trigger-ready and without Screens is easy if you just employ a simple design pattern.  For my Flows, I keep the data and logic elements like decision branches and record manipulation, which you could call the 'controller' or core part of the Flow, in a vertical path down the center of my design.  For the Screens, which you could call the 'view' part of the Flow since that is the part Flow users can see, I push those elements out into columns to the far left and far right of my controller elements.  For example, a simple Flow would have two columns, the leftmost would be Screens only, the rightmost would be logic and record manipulation only.  A more complicated Flow might have Screens on both the rightmost and leftmost columns, like the one illustrated here.

By keeping Screens in their own columns away from the logic, they can be quickly selected within the Flow designer with a single motion and deleted from the design  when you are satisfied with the functionality so that the Flow can be used as part of a workflow without user intervention.

Flows can be executed by Visualforce pages, by other Flows, and by users; with Summer '14 Apex will be able to execute Flows as well.  They can be executed  from record detail pages using buttons, links and Actions; from Tabs, Force.com Site pages and from Global Actions.  Keeping the Flows small and function-specific will help make them reusable in these many scenarios so that you don't have to recreate the same functionality in multiple Flows.  For example, a Flow that operates from a workflow rule and also from an Action on a record detail page might have the 'controller' elements in one Flow that can be called by a Flow Trigger as well as by a Visualforce page that would provide some 'view' context for the user executing the record Action.

Even clicks-not-code administrators can be creative developers once they start designing for multiple uses and considering the debugging process and Model-View-Controller implementation patterns.  And as you become more comfortable thinking like a developer, you may eventually decide to learn more about the Debug Logs and Developer Console.  But with Flow, you might even decide you don't need those to create complex business solutions that work well for your users.


  1. Great article!!! One of the great new features of Summer14 is that Trigger Flows can now be run from the designer using the Run button. This allows you to use the "de-bug" screens in your flow during development. You simply need to assign a default value to your input variable to start the flow. And then remove the screens before running or testing as a trigger flow.

  2. James, thanks for the reminder about having default values. That's helpful, and can be done a couple of ways.

    If the variable is for a record ID, 'hard-coding' an ID isn't best practice. If you plug in a default value to test a Flow using a specific record ID, that record may not be available or work the way you need it to the next time you edit and test the Flow.

    Rather than hard coding the ID as a default value, consider using a Record Lookup element at the beginning of the Flow that kicks off only when the Record ID is blank or that you link into the Flow only during testing. You can also add an ending condition off the lookup for testing when no records meet the criteria.