·
2 min read

How to: State machines in Microsoft Flow

Read about how to build State Machines inside Microsoft Flow with this great post from Serge Luca.

Here is the (simplified) problem : A user 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.

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:

Here is the detail of a specific state:

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 :

So you get this :

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.

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).

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.