Andy in the Cloud

From BBC Basic to Force.com and beyond…

Introducing the Flow Factory

7 Comments

Flow is a great technology for providing a means for non-coders to build functionality. More so than any other point and click facility on the platform, even Process Builder. Why? Because it offers a rich set of Elements (operations) that contain conditional branching, loop and storage of variables. Along with the ability to read or update any object (API accessible) you like. Its almost like a programming language….

Ironically, like Apex, it is missing one increasingly asked for feature… being able to call another Flow that is not known at the time your writing your calling code. Such as one configured via the amazing Custom Metadata… Basically a kind of Apex reflection for Flow. Often the workaround for this type of problem is to use a factory pattern.

As i highlighted in this prior blog on calling Flow from Apex, the platform does not yet provide this capability to change this please up vote this idea. Well at least not in Apex, as there exists a REST API for this. Meanwhile though back in the land of Apex, it occurred to me when building the LittleBits Connector last year. As workaround i could generate a factory class that would workaround this.

I have created Flow Toolbelt library (GitHub repo here) and package (if you want to install that way) which takes last years solution and lifts it into its own smaller package. The Flow Factory tab discovers the Flows configured in your org and generates the required factory Apex class. If you add or remove flows you need to repeat the process.

FlowFactory.png

Once this has been Deployed you can use code like the following. Passing in the name of your Flow. Note this is a WIP version of the library and needs more error handling, so be sure to pass in a valid Flow name and also at least an empty params Map.

Flow.Interview flow =
  flowtb.FlowFactory.newInstance('TestA', new Map<String, Object>());
flow.start();
System.debug(flow.getVariableValue('Var'));

I think this concept can be extended to allow Flow to run from other Apex entry points, such as the recently added Sandbox Apex callback. Allowing you to run Flow when your Sandbox spins up. Let me know your thoughts, if this is something useful or not.

 

 

7 thoughts on “Introducing the Flow Factory

  1. This sounds great. I have a possible use case. Please tell me if I’m reading this correctly. I have an approval process. At a certain step a user needs to fill in additional information on the record. Currently I’ve written an apex trigger that throws a warning explaining what needs to be filled in. Unfortunately, it’s just an apex AddError warning and it’s not very user friendly. I’d love to be able to call a flow screen and have the user fill in the three fields, then approve the record. So will installing the flow factory allow me to write a flow for just those fields, then call it from the trigger instead of throwing up the ugly error message? That would up level the user experience considerably.

    Thanks!

    Arnold

    • Sadly in your scenario you could only call a Automation Flow (or Flow without a UI). Triggers cannot throw up a UI regardless of Flow or not. The addError method is the only way to communicate. If you want some more custom process you would have to resort to a Visualforce page with some Apex logic and use Aciton Override to override the Edit action or add a Custom Button.

  2. I would love to boost declarative tools like yours in all scenarios where Apex is not necessary. These get increasingly more but testing frameworks / best practices are lacking. How do I ensure QA without wasting all time saved by clicks because I have to setup and create e.g. selenium based test suite. ‘Let me quickly create sth for you’ directly in production seems also to be also encouraged by the lack of test coverage. We had a talk at the circle of success over here , and we are all lacking ideas.

    • Yes I agree, any solution declarative or code needs a testing strategy. Hopefully the net effect regardless when using declarative is still a saving compared to a fully coded solution. When I speak to some admins that use declarative, they have learnt enough of apex to write apex tests for validation rules, formulas, flows etc. There is also some commercial solutions (I got from Google) such as http://www.provartesting.com/index.html

  3. Pingback: Dynamically Calling Flows in Apex | Andy in the Cloud

  4. Pingback: Calling Flow from Apex | Andy in the Cloud

  5. I think line 2 in the example needs to read as:

    flowtb_FlowFactory.newInstance(‘TestA’, new Map()); // ‘underscore’, not ‘dot’ after ‘flowtb’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s