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!

1 REPLY 1

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.