How to push return group members in an flow variable inside flow designer?

saint
Tera Expert

I’m working in ServiceNow Flow Designer and need guidance on handling group members across multiple groups.

Requirement:

  • I have two different groups
  • I want to collect the members from each group into two separate variables (preferably as comma‑separated values, e.g., sys_ids)
  • After that, I need to find the common users between both groups
  • Finally, I want to create a comma‑separated list of common user sys_ids and use it to send approvals to all those users

Any guidance or best practices on implementing this in Flow Designer would be greatly appreciated.

Thanks in advance!

1 REPLY 1

Naveen20
ServiceNow Employee

Try this

Step 1 — Look Up Members for Each Group

Use two separate Look Up Records actions, both querying the sys_user_grmember table:

  • Action 1: Table = sys_user_grmember, Condition = group = <Group A sys_id> — returns all member records for Group A.
  • Action 2: Same table, Condition = group = <Group B sys_id> — returns all member records for Group B.

Both return arrays of group member records.

Step 2 — Build Comma-Separated Sys_ID Strings

After each lookup, use a For Each loop over the result set and a String flow variable to accumulate the user sys_ids. Inside each loop iteration, use an Update Flow Variable action to append the current record's user.sys_id to your string variable with a comma delimiter.

You'll end up with two string variables like:

  • group_a_members = "sid1,sid2,sid3"
  • group_b_members = "sid4,sid2,sid1"

Step 3 — Find the Intersection (Common Users)

This is the trickiest part since Flow Designer doesn't have a native "array intersection" action. You have two solid options:

Option A — Script Step (recommended)

Add a Script Step action with inputs for both comma-separated strings and output a new string of common sys_ids:

(function execute(inputs, outputs) {
    var listA = inputs.group_a_members.split(',');
    var listB = inputs.group_b_members.split(',');

    var common = listA.filter(function(id) {
        return listB.indexOf(id.trim()) !== -1;
    });

    outputs.common_members = common.join(',');
})(inputs, outputs);

Define common_members as a String output variable on the script step.

Step 4 — Send Approvals to Common Members

Once you have the common_members comma-separated string, you have two approaches depending on your approval pattern:

For individual approvals (one per user): Split the string in a script step into an array, loop through it, and use the Ask For Approval or Create Approval action for each user.

For a single group-style approval: Use the Ask For Approval action with the "Anyone Approves" or "Everyone Must Approve" rule. You can feed the comma-separated sys_id list directly into a Look Up Records action on sys_user with the condition sys_idIN<common_members>, then loop over results to generate individual approval records. Alternatively, create a temporary approval group programmatically via a script step.