Loop through a group and capture email address in a flow variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hello!
I'm trying to loop through a group and capture all email addresses of the people in that group. Here is what I've done that is not working:
1. Create a flow variable: u_approval_members (WORKING)
2. Use "Lookup Records" to loop through all members of a group (WORKING)
3. Create "FOR EACH" loop to cycle through the records found in #2 above. (WORKING)
4. Within the loop, use LOG INFO to show me the email address of each person it found (WORKING)
5. Within the loop, use "Set Flow Variable" using the script below to capture all the email addresses in the string I created in #1 (NOT WORKING)
var currentList = fd_data.flow_var.u_approval_members || ''; // Fallback to empty string
var email = current.user.email || ''; // Fallback to empty string
if (email) { // Only append valid emails
return currentList ? currentList + ',' + email : email; // Append with comma or start list
}
return currentList; // Return unchanged if no email
Any ideas? ?Thanks everyone!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
I don't believe the current. syntax is valid in Flow Designer, unlike everywhere else in ServiceNow, and the email field is a different type/extension of string, so you need to be sure to force it to a string. Depending on how you have the flow triggering, and what's working for you in the other steps, it would look something more like this:
var email = fd_data.trigger.user.email.toString() || ''; // Fallback to empty string
Try logging this script variable to confirm it's getting populated, or maybe you can see this with full debugging/reporting on.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
53m ago
Hi @brianhofmei ,
The issue you're encountering is likely due to overwriting the flow variable in each iteration, rather than appending the email addresses....
To capture all email addresses from group members in a ServiceNow Flow Designer loop, initialize a string variable (e.g u_approval_members) before the loop. Within the loop, append each member's email to this variable, ensuring each email is separated by a comma. Use the following script in a Flow Script action:
var currentList = fd_data.flow_var.u_approval_members || ' '; // Fallback to empty string
var email = current.user.email || ' '; // Fallback to empty string
if (email) {
// Append the email to the current list, separated by a comma
return currentList ? currentList + ',' + email : email;
}
return currentList; // Return unchanged if no email
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3m ago
Hi @brianhofmei
I just modified the flow steps and script as below and got the desired output.
Please try this and see how it goes..
Set Flow Variable script:
var current_flow_variable_value = fd_data.flow_var.u_approval_members;
var arr =[];
if(str!=''){
arr.push(cuurent_flow_variable_value);
}
arr.push(fd_data._2__for_each.item.user.email.toString()); //STEP NUMBER MAY VARY
var mail_ids = arr.join(",");
return mail_ids;
Flow actions steps:
Output:
Regards,
Siva