Power Automate: State Machine Workflows

Introduction

Sequential workflows follow a step-by-step process that does not require the flow to go back to a previous state. However, there are processes where you will need to transition back and forth between various states or stages which we usually call the State machine workflows. State machine workflows provide a modeling style with which you can model your workflow in an event-driven manner. So, in general, State machine workflows move from one state to another (backward or forward) depending on an event which can generally be a user interaction in the form of approval in case of an approval workflow.

In this article, we will see how to implement a State Machine pattern in Power automate as it is not available by design

Business Use Case

Let’s take a business use case where we have a Publishing Approval Process in place that needs to go back and forth between various states before the content moves to the final published state.

We will have the below States that defined the stage at which the Document is in the approval flow:

  • Draft Version: Initial version submitted by the Initiator
  • Resubmit Draft Version: Content Creator has rejected the initial Draft Version and it is sent back to initiator for resubmission
  • Editor Version: Content Creator has completed the detailed content creation and moved it to Editor for editing
  • Published Review Version: Editor has completed the editing and has moved it to Publisher for review
  • Published Version: Published has completed the Proof reading and is in a ready to Go Live state

We will define the approval process in the SharePoint List as below:

  • An article is submitted with a basic content in the SharePoint List which triggers the flow
  • When the Article is submitted, the Status will be in Draft Version and it sends an Approval notification to the Content Writer who is supposed to add detailed content to the article. Once he has completed the content creation, he approves it and the status moves to Editor Version. If there is more information needed, he rejects the approval process which sends it back to the initiator with a Resubmit Version Status who will get an approval notification to either Resubmit it or Withdraw the Article. If he makes the changes and Resubmits it, it will move back to the content creator for review again. Or else if he withdraws it, the workflow ends.
  • In case the Content Creator approved in previous stage, the Editor gets an approval notification, and he goes through the content creator’s work and edits the document. If there is more information needed, he will reject it and the document goes to the previous state – Draft State where the Content Editor will get notification to work on it. If the Editor approves the changes, document moves to the next state – Publisher Review Version
  • Once the publisher gets the notification for review, he will do a final proof reading and either approves it which will move it to final Published state or rejects it which will move the document back to the Editor Version State for further editing.

Implementation

To store the documents, we will create a document library with below columns

Graphical user interface, text, application, email

Description automatically generated

Publish Status will be a choice column that will have the various state values that will be used to differentiate the stage the workflow is in:

  • Resubmit Draft Version
  • Draft Version
  • Editor Version
  • Published Version
  • Publisher Review Version
  • Rejected Version

The Overall Flow would look like below. Let’s take a look at the details of the implementation

A screenshot of a computer

Description automatically generated

We will be triggering the flow when a new document is created with basic content. As the next step, we will declare a state variable – ContentPublishStatus which will be checked for switching between various states. Initially, when the document is created, we will set the default value to Draft Version.

Graphical user interface, text, application, email

Description automatically generated

The next step is the major state switching action where we will add a Do Untill Action and nest a Switch Action inside it. The Do Untill will run until the ContentPublishStatus value is updated to the value – Published Version

Graphical user interface, application, Teams

Description automatically generated

Within the Switch, we will evaluate the value of ContentPublishStatus to either (Draft Version/Editor Version/Publisher Review Version/Resubmit Draft Version) and the branch to the corresponding Case section.

Draft Version Review Case

If the switch variable value equals Draft Version, it will enter the below case where it will send an approval notification to the content writer for adding further details to the document.

Graphical user interface, application, Teams

Description automatically generated

The Content Writer can go to the link and review the initial version submitted by the reviewer and decide whether to go ahead with the further content writing process or sent it back to the initiator to submit a proper draft version. In case he decides to go ahead, he will take up the document and do the content writing and eventually click on Approve. If he decided otherwise to get a more proper draft version, he will Click on Reject in the approval mail that he had received.

A screenshot of a computer

Description automatically generated

We will check the approval status using the condition action and if Content Writer has approved, we will set the switch variable to Editor Version so that in the next loop, the control will enter the Editor Case and update the File property with the same status in the SharePoint Library. In case the Outcome is a Reject, we have to set the switch case variable to Resubmit Draft Version so that in the next loop, it will enter the Resubmit Version Review case and the initiator will be notified of the rejection.

Resubmit Version Review case

Before moving to the Editor Case, let’s check the Resubmit Version Review case which will be called if the Content Writer has rejected the Document. The resubmit version review case will use a Custom Approval action so that we can use custom response options other than Approve/Reject and we will add the custom option Resubmit and Retract. In case the initiator decides to make further changes, he will make the updates to the document and click on resubmit, or else if he decides to withdraw the document from the publishing process, he will click on retract.

Graphical user interface, application, Teams

Description automatically generated

If the outcome evaluates to Resubmit in the condition action, we will set the switch variable to Draft Version which will move the document to the Content Creator (First Step) again in the next loop. We update the file properties with the same status in the document library. In case the Outcome evaluates to Retract, then we will set the switch variable as Rejected and update the file properties in the SharePoint library.

A screenshot of a computer

Description automatically generated

Editor Version Review Case

In the previous section, we saw the Resubmit Version Case when the Content Writer rejects the approval process. In case the Content Writer has approved, it will move to the editor version review case in the subsequent loop where the Editor will get notified about the new document that is in the queue for editing. He can decide to take it up for editing and Click on Approve in the mail he received upon completion of editing which will move the document to the next stage of Publisher Review or else he can Click on Reject which will send the document back to the Draft Version Review.

Graphical user interface, text, application, email

Description automatically generated

In case the Outcome evaluates to Approve, we will set the switch variable to Publisher Review Version and update the file properties with the same status so that in the next loop, the control enters the Publisher Review Version. Otherwise, if the outcome evaluates to reject, we will set the switch variable to Draft Version and update the file properties with the same status so that in the next loop, it will move back to the Draft Version Review.

A screenshot of a computer

Description automatically generated

Publisher Review Version

If the Editor had approved in the previous loop, it would enter the Publisher Review Case and sent out an approval notification to the Publisher to do the final review. He can choose to review it and approve the document or else reject it.

Graphical user interface, text, application, email

Description automatically generated

If the Outcome evaluates to Approve, then that implies the document is ready to go live and we update the switch variable to Published Version which is the exit condition of the loop, and in the next iteration, it will exit the loop. If the Outcome evaluates to Reject, then we set the switch variable to editor version so that in the next loop, it will go back to the previous case where the editor will have to edit the document further.

A screenshot of a computer

Description automatically generated

Summary

Thus, we saw how we can implement a state machine workflow that has to travel back and forth between various states based on user input.

Related Articles

Author

Author

Priyaranjan KS is a Modern Workplace Architect primarily focused on developing and architecting solutions around Office 365,Power Platform and Azure.He is also a Microsoft Most Valuable Professional(MVP) and a Microsoft Certified Trainer(MCT)

Latest Articles