Sunday, January 9, 2022

Power Automate: Multiple Approvers Approval Exceeding Threshold (Generic Less Code)

 Hello Friends,

Welcome back with another post on Power Automate. In our previous post, we have discussed above managing multiple level of approvals where this level may go beyond 10, 20 ...

Power Automate: Multiple Approvers Approval Exceeding Threshold

 We have achieved this functionality successfully. However, the only issue is that we have to repeatedly write the same login for each level. Can we do something which would be generic in nature and allow us to write logic only once and then it will repeatedly execute until finish. Let's try-

  1. Taking example of previous post whose link is given above, we will first create a Power Automate (Automate cloud flow >> When an item is created).
  2. Initialize below variables-
  3. Save the flow to avoid and mishapenning. It's a good practice to save your work at regular intervals to avoid any loss due to any incident. 😊

  4. Now, we will get the items from ApproversMaster list (see my previous blog post to read about ApproversMaster list). For this we will use "Get Items" action. We will apply filter upon Country, State and RequestedAmount. We will also apply Orderby on ApprovalLevel as ASCENDING.
  5. Now, we will check, if this action has any outcome, means if there are any Approvers available. If YES, the get the ApprovalLevel of last item; add 1 to it and assign it to intTotalIterations variable.
  6. Now, the real battle begins. From here, we will use a looping structure (Do until) which will work upon intLevelNumber.
  7. For each level-
    1. It will first check if there is an ApproverLevel available in ApproverList for the LevelNumber iteration. If YES, assign Approver Name, Approver Email, Delegate Approver Name, Delegate Approver Email to respective variables.
    2. Next, it will check if blnProceedFurther is TRUE, strApproverEmail is available (means should not be NULL or BLANK). Then-
      1. Update (Send an HTTP request to SharePoint) Approver Name in PurchaseRequest List Item.
      2. Start Approval Process (Start and wait for approval)
      3. Set blnProceedFurther (means next iteration approval should be executed or not) by TRUE/FALSE based upon Approve/Reject as Outcome of Approval Process.
      4. Update (Send an HTTP request to SharePoint) ApprovalStatus & Approval Date Time in PurchaseRequest List Item.
    3. Reset respective Approver Name, Approver Email, Delegate Approver Name, Delegate Approver Email variables to NULL.
    4. Increment intLevelNumber by 1.
  8. Let's begin. Apply looping structure (Do until)-
  9. For each level-
    1. Check ApprovalLevel and assign Name & Email variables-
    2. if blnProceedFurther is TRUE and strApproverEmail is available-
        1. Update ApproverName in List (Send an HTTP request to SharePoint). Now, here we will make all the executions pure dynamic-
        2. Start Approval Process (Start and wait for approval)-
        3. Set blnProceedFurther-

          1. This equals function will result either true or false based upon the condition. So, if the Outcome is Approve, we need to proceed with next level of approval otherwise termiate.
        4. Update Approval Status and Date in List (using Send an HTTP request to SharePoint)-
        5. This is how the all these steps looks like-

    3. Reset all 4 Name / Email variables-
    4. Lastly, we will increment the intLevelNumber by 1.

  10. The complete flow will be-
  11. That's all. It's time to test now. Before execution, let me inform you the flow-
    1. Level 1-
      1. Approver: Ram Vinay
      2. Delegate: Lokesh Kumar
      3. Limit: $0
    2. Level 2-
      1. Approver: Lokesh Kumar
      2. Delegate: Amit Sharma
      3. Limit: $0
    3. Level 3-
      1. Approver: Amit Sharma
      2. Delegate: Sarvesh Singh
      3. Limit: $500
    4. Level 4-
      1. Approver: Sachin Jain
      2. Delegate: Lokesh Kumar
      3. Limit: $15000
    5. Level 5- (Will Be SKIPPED As LIMIT Is Higher Than $20000)
      1. Approver: Sarvesh Singh
      2. Delegate: Ram Vinay
      3. Limit: $25000
    6. Level 6-
      1. Approver: Lokesh Kumar
      2. Delegate: Ram Vinay
      3. Limit: $12000

  12. Let's create an item-
    1. Title: New Project Work
    2. Country: India
    3. State: Uttar Pradesh
    4. RequestedAmount: 20000
  13. Save the item.
  14. Workflow gets started and it has updated Approver1 Names in list-
  15. Both approver & delegate received the mail-
  16. Let delegate will approve the request. The moment, delegate had approved the request, flow had updated it's status and date in system.
  17. After then, it had started the next level of approval-

  18. That means, one round of our workflow has completed. It is running fine. Now, I will just post screenshot of each level-
    1. Post Assign
    2. Post Approval
  19. Level 2-
    1. Post Assign-

    2. Post Approval-

  20. Level 3-
    1. Post Assign-

    2. Post Approval-

  21. Level 4-
    1. Post Assign-

    2. Post Approval-

  22. Level 5-
    1. Post Assign-

    2. Surprised? Why it is blank? Because, as per our logic, only approvers having limit less than or equal to the RequestedAmout will be eligible for approving the request. Level 5 person has approval limit higher than that amount. Hence skipped. Instead, Level6 should receive the Approval request. Let's check.
  23. Level 6-
    1. Post Assign-

    2. Post Approval-

  24. This way, you can implement any number of Approval Stages.
  25. IMPORTANT Points To REMEMBER-
    1. Number of levels should be always less than the threshold of Do-Until loop.
    2. All fields you are going to update in this process (ApproverName, ApprovalStatus, ApprovalDate) must bear similar pattern and similar internal name in list. i.e.-
      1. Approver1Name
      2. Approver1ApprovalStatus
      3. Approver1Comments
      4. Approver1ActionDate
      5. .
      6. .
      7. .
      8. Approver5Name
      9. Approver5ApprovalStatus
      10. Approver5Comments
      11. Approver5ActionDate
      12. .
      13. .
      14. .
      15. Approver15Name
      16. Approver15ApprovalStatus
      17. Approver15Comments
      18. Approver15ActionDate
      19. .
      20. .
      21. .

With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.