Lisa Holenstein
ServiceNow Employee
ServiceNow Employee

Workflow Automation CoE >Flow Designer > Approvals > Scripted Approval

 

Why Scripted Approvals?

Before you consider scripting your approval, please check out this article and Platform Academy recording about Approvals in Flow Designer. There's a good chance you don't actually need to script the approval but can just use the "Ask for Approval" Action with the low code conditions. Scripting should always be your last resort.

Article: https://sn.works/CoE/FlowApproval

Academy: https://sn.works/platformacademy/35 

 

When creating Approvals for Tasks or specifically Catalog items, the provided options of requesting approval from a specific user or group is not always sufficient to cover your use case. Sometimes you just need that extra condition to make sure the correct people may approve or reject.

In the "before Flow Designer times", this would mean Scripted Approvals. The Flow Action "Ask for Approval" also allows you to script approval rules, but it's not obvious at first glance which kind of input is needed to create the approval. The introduction of Flow Variables allows us to create approvals with an absolute minimum of scripting.

 

Approval Syntax

The Flow Action "Ask for Approval" will take a return value created from several components: a ruleset, a rule, the who, and sys_ids. The square brackets take a list of comma-separated sys_ids.

Syntax
ruleset+rule+who+[sys_id list]+(optional)ORruleset+rule+who+[sys_id list]
Approval Rulesets
Approves
Rejects
ApprovesRejects
Rules
Any = Anyone approves
All = All users approve
Res = All responded and anyone approves
% = % of users approve
# = number of users approve
Who
U = Users
G = Groups
 
Examples:
Approval Script
Approved by any User
ApprovesAnyU[user_sys_id,user_sys_id]
Approved by 50% of Group
Approves50%G[group_sys_id]
Approved by 2 of Users or of Group
Approves2#U[user_sys_id,user_sys_id]G[group_sys_id]
Rejected by all Users
RejectsAllU[user_sys_id,user_sys_id]
Rejected by any user in Group
RejectsAnyG[group_sys_id,group_sys_id]
Approved or Rejected by any User
ApprovesRejectsAnyU[user_sys_id]
Approved by Group or Rejected by any User
ApprovesAnyG[group_sys_id]OrRejectsAnyU[user_sys_id]
All responded and anyone approves in Group
ApprovesResG[group_sys_id]

 

Flow Designer & Flow Variables

Now, how do we query the right users/group and add it to the flow logic? We'll want the help of a flow variable to capture the user sys_ids.

 

Scenario:

For this example I created a Catalog item with a User variable. I want to look up the group members [sys_user_grmember] of a specific group and only add those that are assigned to the same company to the approvers in my flow. I'm using the demo data group "Network" which has 5 members from "ACME North America" and I added one more member (Aileen Mottern) who is assigned to ACME Italy. I expect my approval to only create entries for the 5 members from "ACME North America" if I choose Beth Anglin in my Catalog Item variable.

find_real_file.png   find_real_file.png

In Flow Designer, I've created a new flow with a "Service Catalog" trigger so I can assign this flow to my Catalog Item.

This is the full flow and I'll take you through the individual steps. I haven't added further logic to go from the approval (just a log statement), because that will be dependent on your use case how you want to proceed based off of the approval status.

find_real_file.png

1) I've created a String flow variable to capture the user sys_ids.

find_real_file.png

find_real_file.png

2) I'm getting the Catalog variable "account_holder" from my Catalog Item to have access to the user's company.

find_real_file.png

3) I use the "Look Up Records" action on the Group member [sys_user_grmember] table to find users in the Group "Network" who have the same Company entry as the user in my "account_holder" variable.

find_real_file.png

4) I added a for each logic and loop through the results from step 2.

5) To create a list of results in my flow variable, I add the variable, a comma, and the user sys_id from the for each loop. This will work even if the string starts with a comma.

find_real_file.png

6) Finally, I'm adding the "Ask for Approval" step. To configure a scripted ruleset, click on the script field next to "Rules".

find_real_file.png

7) The script field needs you to return a string that is created based on the syntax above. In my case, the approval will be approved or rejected if any users from that string of users will approve or reject. I'm using my flow variable and concatenate the list of sys_ids into the return string.

var approvers = fd_data.flow_var.users;
return "ApprovesRejectsAnyU["+approvers+"]";

find_real_file.png

😎 Creating a new REQ from my catalog item will result in a RITM with my 5 expected approvers. The list does not include Aileen Mottern.

find_real_file.png

 

I hope this blog is useful to you and helps you create your own custom approvals with Flow Designer and Flow variables. I'm looking forward to your feedback and maybe you also want to share your use cases with us.

 

Center of Excellence Navigation

  1. Workflow Automation - Center of Excellence
  2. Workflow Automation Migration Considerations
  3. What's new for Workflow Automation?
  4. Resource Hubs
    1. Flows
    2. Decision Tables
    3. Playbooks
  5. FAQ
  6. Checklist
  7. Training
  8. Workflow Academy
43 Comments
msowmiya
Tera Contributor

Hi Lisa,

 

This article is super helpful. I am looking for your help on the following scenario.

I tried using the scripted approvals to generate multiple user approvals dynamically.

Case : To add users into multiple groups. We have used the multirow variable set to collect user name, group name and manager name. We have to get approvals for all the group selected in each row

User NAmeGroup name

 

Manager name

User 1, User 2 Group1

Mr.A

User1, User3Group2

Ms.B

User2, User3Group3

Mr.C

 

Can you share your thoughts on how to generate approvals for each line item in the Flow designer.

Sails
Kilo Sage

Exactly what I was looking for to suggest my PO, as they don't typically believe unless documented somewhere 😉 Thank you @Lisa Holenstein 

dosualdo1
Tera Expert

Where can I find documentation on this? I'm trying to use more than one rule, but I get an error of "invalid rule syntax". 

The rule that my script is returning is: "ApprovesRejectsAllU[7b5fe0091b712190d6050fe1f54bcb0e,9dafe4091b712190d6050fe1f54bcbae]ORApprovesAllU[7b5fe0091b712190d6050fe1f54bcb0e,bfcf2c091b712190d6050fe1f54bcb44]"

Lisa Holenstein
ServiceNow Employee
ServiceNow Employee

Hi @msowmiya,

I believe you can access the MRVS variables with the "Get Catalog Variables action". Then you can use a for each to loop through the variables, look up the users / groups by sys_id, then request an approval with these. I don't think you even need to script the approval logic.

LisaHolenstein_0-1680097355550.png


Hi @dosualdo1,

I'm not 100% sure, but the operator may be case sensitive, so it would need to be 'OrApprovesAllU'. Or you can try with a vertical pipe | , which is what is happening when I create a flow with an OR approval.

Example: 

'ApprovesRejectsAnyU[62826bf03710200044e0bfc8bcbe5df1]|AnyG[287ebd7da9fe198100f92cc8d1d2154e]'

jagabandhu padh
Tera Contributor

Hi @Lisa Holenstein ,

 

Thank you for this post number of my queries were clarified but have one requirement  where i look up sysapprover_group table and fetches all the "not yet requested" approval status records and want to use that group in approver . i am not getting the look up data in next "ask for approval" action . am i missing something in between . also if i receive multiple groups in looks up can i use the configuration each from one group should approve ?

 

Thanks in advance .

 

Jaga 

jagabandhu padh
Tera Contributor

Hi @Lisa Holenstein ,

 

How to use manual group approval in flow designer . i am able to use Manual user approval but can i have any option to use Manual group approval where the added group from group approval will trigger after certain state or something .

Thanks and Regards

Jagabandhu Padhy

gjz
Mega Sage

Hi Lisa,

I believe I'm missing some concepts in this example, would you please clarify for me?

1. You added a grid of the types of approval actions along with a "script".  Are these scripts already available for use?

gjz_0-1681924138468.png

2. How do you act on an approval or rejection if a scripted rule is used?  For example, if someone rejects it I would like to stop the flow and send a rejection email to the user who requested the item.

Thanks!

 

Lisa Holenstein
ServiceNow Employee
ServiceNow Employee

Hi @gjz ,

 

those scripts are examples of the syntax, so you can derive the right configuration for your use case.

For subsequent steps, you can use if-then-else logic as with any other use of the Ask for Approval action, it will output an approval state that you can build your conditions on.

 

However, I would always recommend to start with the Ask for Approval action without scripting, because it already allows for pretty complex approval use cases. You only need to use the scripted approvals, if you want to concatenate a list of users from a prior look-up that you can't do without scripting.

 

LisaHolenstein_0-1681972018116.png

 

 

Keep an eye on the Workflow Automation Center of Excellence (https://sn.works/CoE/StartFlow), I will be posting a detailed article about approvals in Flow Designer next week, and we'll also have a Platform Academy webinar about this topic next week Thursday. https://sn.works/platformacademy/35 

 

gjz
Mega Sage

Thanks for the information, Lisa.

 

If I understand you correctly, your recommendation is to get a list of approvers in a previous step (where it's easier to use a script) and use the output of that action as the approvers in the approval step.

 

I am looking forward to the next Platform Academy, it would be really helpful if you could include an example just like this.  In my community search, it's apparent I'm not alone in needing to script an approval due to requirements at my company; it seems a lot of companies have this types of requirement.

jhec
Tera Explorer

Hello Lisa,

Following your excellent tutorial, I managed to generate the following rule:

ApprovesRejectsAllU[8c6c1fdfdbd3b30067ba115e6896190a]&ApprovesRejectsAllU[87401167dbdbb30067ba115e689619b4]&ApprovesRejectsAllU[7e6b39231b173700e53dc9506e4bcb29]&ApprovesRejectsAllU[8c6c1fdfdbd3b30067ba115e6896190a]&ApprovesRejectsAnyG[0fb969958716ed18dd76873e8bbb358d]&ApprovesRejectsAnyG[cf723d19871aed18dd76873e8bbb354d]

Basically, I expect that the first 3 users must approve (the fourth one is a duplicate), and that one member of each group must approve as well. 

However, when duplicated user approves, the group approvals are not required any longer. What is wrong in my understanding ?

Thanks