cancel
Showing results for 
Search instead for 
Did you mean: 

Simulate state machines with Microsoft Flow

Most workflow engines & tools I know (Nintex, K2, DataPolis, .Net Workflow) support state machines; at some point your workflow is so complex that you really really need it. I’m a very strong supporter of state machines, but my favorite tool, Microsoft Flow doesn’t support it (yet?). Only sequential workflow is supported so far.

 

In this blog post, I will illustrate how I solved this specific issue and I believe this could become a pattern for your future flows.

 

Here is the (simplified) problem :

A use creates a document in SharePoint. This document must be validated by a person called “Val”;  when Val has validated the document, it must be validated by the “secretariat”; when this is done, it must be validated by the “Boss”; then the document will be signed by the boss (electronic signature) , transformed into PDF and published to external users. Of course the boss can reject the document and send it to the secretariat or to val; Val can also reject the document.

 

Basically the workflow looks like this :

val

Good luck to create this with Microsoft Flow! Mind you, you might end up creating plenty of not maintainable loops, conditions, approval and you will scratch your head (as I did). And what if you don’t just have 3 approval levels, but 10 or 15..or 100 (…just kidding) ?

 

When you are desperate you become creative and you start thinking out of the box:

Here is what I did :

  • create a loop
  • create a switch
  • each branch of the switch is a state
  • specify where you come from and where you want to go
  • in the loop specify how you exit the loop

That’s it : – )

 

Here is the whole implementation (click to zoom)

statemachine

 

Here is the detail of a specific state (click to enlarge) :

 

statemachinebranch

 

You can extend each branch (state) if needed by managing the timeout : if the Secretariat doesn’t react on time you can ask it to approve again :

Timeoutsecretariat2

 

So you get this :

 

Timeoutsecretariat

 

Now I know you next question : how does the system know that when the Boss rejects the document, the request must go to Val or to the secretariat ?

 

Indeed in the approval form, we only have 2 buttons so far : approve or reject. I know that custom forms will be available in the future, but in the meantime we need to rely on the comment box in the approval form : if the boss types “VAL” or “val” or “V”, the flow will go to VAL, otherwise it will go to the secretariat. He can even type V or Val followed by a comment. This is totally acceptable until custom forms are made available and I believe this is super important (are you listening Flow team ?   ;-).

 

bossapprove

 

One of the downside of this approach you will face with this approach is that the until loop is opaque when the Flow is running, you don’t see what’s going on (please Flow team, fix this asap).

dountil_empty

 

However you can still see the current flow state by storing in a custom “status” column in your SharePoint List or Document library. And if you want more (history, statistics, analytics, check one of my previous blog post “Flow approval escalation with analytics“).

 

In summary: each state become a branch in a switch. This is simple, easy to implement, easy to maintain, it scales and it works in the current implementation of Microsoft Flow. This approach also scales because can can easily add many states (or branches) and each branch can also be implemented as a Service Flow (nested Flow) if needed.

Comments

thanks! there go my plans for moving from spd workflows to flow.  are ther user voices where i can vote on the issues noted here (state machine and forms)?

Hi Serge,

 

Great article, super easy to follow! I found this through the Flow blog that was created off your post. I've been doing this as well, to bypass the 30 day flow run timeout limitation.

 

One of the things to be aware of is that the number of states has considerable impact on the loadtime for the flow editing page. One large process I had included over 9 states, with some states requiring multiple parallel and sequential actions and approvals, and catches for errors/timeouts, and editing it was quite frustrating. So if you have a really complex process end-to-end, there could be benefit in triggering sub-flows in each state using HTTP, and containing the actions for that state in the sub-flow.

Thank you very much for this all information.

 

Could you kindly tell me or sharing the Variables images (In pink)? Please!

I hope that SPD will not decommissioned before we have state machine feature in Flow

Meet Our Blog Authors
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Cambridge UK Power Platform User Group Leader, Technical evangelist and speaker. Always says yes to coffee! #LetsGetCoffee
  • Passionate #Programmer #SharePoint #SPFx #Office365 #MSFlow | C-sharpCorner MVP | SharePoint StackOverflow, Github, PnP contributor