Finding Common Members Between Two Groups in Subflow

saint
Tera Expert
Hi Experts,
 
I’m currently retrieving members from two different groups using lookup records and passing them as inputs into a subflow as reference.sys_user types. This process returns the sys\_ids of all users in each group.
 
I’m looking for a way to identify the common members between these two groups and set the result in an output field. I’ve tried several scripts and variable types, but the result is either blank or errors out.
 
If anyone has implemented something similar, I’d really appreciate your guidance.
 
Regards,

 

3 REPLIES 3

pavani_paluri
Kilo Sage

Hi @saint ,

 

Since you have users from both groups, you just need to find the common ones between the two lists.
Add a Script step in your subflow and use logic like this:

var commonUsers = [];

for (var i = 0; i < inputs.group1_users.length; i++) {
if (inputs.group2_users.indexOf(inputs.group1_users[i]) > -1) {
commonUsers.push(inputs.group1_users[i]);
}
}

outputs.common_users = commonUsers;
Takes users from Group 1, Checks if they also exist in Group 2
If yes → adds them to output, So you’ll get only common members

 

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

Tanushree Maiti
Mega Patron

Hi @saint 

 

Add a Script Step to evaluate the overlap and set up the following inputs and outputs:

Inputs:

  • group_a_users: Map this to the sys_id list from your first Look Up Records.
  • group_b_users: Map this to the sys_id list from your second Look Up Records. 

Output:

  • Name: common_users
  • Type: String (or Array.String depending on subflow requirements)

 

Sample code/Not tested:

 

(function execute(inputs, outputs) {

    var arrA = inputs.groupA ? inputs.groupA.split(',') : [];

    var arrB = inputs.groupB ? inputs.groupB.split(',') : [];

    var arrayUtil = new global.ArrayUtil();

    var common = arrayUtil.intersect(arrA, arrB);

    outputs.common_users = common.join(',');

})(inputs, outputs);

 

You can then map this output variable directly to your subflow’s output field

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

haseena ayesha
Giga Guru

Hi Saint,

You can try this:

Convert group members into sys_id lists → compare them in a Script step → return the common users as a subflow output.

 

Step 1: Get users from both groups (Main Flow)

1. Use Lookup Records
Table: sys_user_grmember
--> Condition: 
group = <Group Abc>
2. From the results, collect:
-user.sys_id
3. Convert the result into a comma‑separated string
Example:
sysid1,sysid2,sysid3
Repeat the same steps for Group B.
Step 2: Create a Subflow
Create a subflow as XXX
Create subflow inputs:
Name                           Type

group1_users     -->      String

group2_users     -->      String

These inputs should contain comma‑separated user sys_ids from each group.

Step 3: Add a Script step inside the Subflow
Add an Action → Script step and use the following logic:

 

Script :
(function execute(inputs, outputs) {

var group1 = inputs.group1_users.split(',');
var group2 = inputs.group2_users.split(',');

var commonUsers = [];

for (var i = 0; i < group1.length; i++) {
if (group2.indexOf(group1[i]) > -1) {
commonUsers.push(group1[i]);
}
}

outputs.common_users = commonUsers.join(',');

})(inputs, outputs);

 

Step 4:  Define Subflow Output
Create one output:

Name                                  Type

common_users     -->   String

This output will contain the sys_ids of users present in both groups.

 

Hope this helps! If it answers your question, please consider marking it as Helpful or accepting it as a solution.
Thank you!