The CreatorCon Call for Content is officially open! Get started here.

Loop through a group and capture email address in a flow variable

brianhofmei
Tera Contributor

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!

8 REPLIES 8

anurampalli
Tera Contributor

hi Brian

 

Where is this script? is it a custom script action? If so, does the script action contain a output variable?

 

Assuming this is a script action you have written and tested with an output variable, instead of "return" set the value of output variable with the concatenated string in your loop.

 

I hope my assumption is correct and this response brings some clarity.

 

otherwise, maybe the following edited snipper will help?

// Get the current flow variable value
var currentList = fd_data.flow_var.u_approval_members || ''; 

// get email from the glide record

var email = current.email || ''; // fallback to empty string

// if email exists append to flow variable

if (email) {
fd_data.flow_var.u_approval_members = currentList ? currentList + ',' + email : email;
}

Best

Anu

Brad Bowman
Kilo Patron
Kilo Patron

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.

 

Hi there.  I tried sendint .toString() but it didn't make a difference.  However I do think I found the fix listed below someone elses response.  Thank you for your response!

kaushal_snow
Mega Sage

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.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/