I want to store unique mail IDs in flow variable

sakthivelsn
Tera Contributor

I want to store unique mail IDs in flow variable and I want to send mail to multiple users who have been removed from groups. I'm quering Group membership table where every user has multiple records this for each condition sends multiple mail

4 REPLIES 4

SumanthDosapati
Mega Sage
Mega Sage

@sakthivelsn 

What is the issue you are facing here? Can you elaborate your question if possible with a screenshot

 

Regards,
Sumanth

Voona Rohila
Kilo Patron
Kilo Patron

Hi @sakthivelsn 

Can you please share the complete requirement with flow details you configured?


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

prerna_sh
Mega Sage

Hi @sakthivelsn 

Could you please elaborate your business requirement.


If my response solves your query, please marked helpful by selecting Accept as Solution and Helpful. Let me know if anything else is required.
Thanks,
Prerna

venkat917181
Tera Expert

Problem I Understood.....

You're experiencing duplicate emails when querying the Group Membership table because each user can have multiple records, and your flow processes each record individually.

Solution: Use Script Action for Deduplication

The most efficient approach is to use a single Script Action that handles querying, deduplication, and email list creation in one step.

Step 1: Replace your current flow logic with this Script Action

(function() {
    try {
        // Query Group Membership table
        var gr = new GlideRecord('sys_user_grmember');
        
        // Add your conditions - modify as needed
        gr.addQuery('group', 'your_group_sys_id');         gr.addQuery('state', 'inactive'); // or your removal condition
        gr.query();
        
        // Arrays for tracking unique users
        var uniqueEmails = [];
        var processedUsers = [];
        
        // Process each record but avoid duplicates
        while (gr.next()) {
            var userSysId = gr.getValue('user');
            
            // Skip if already processed this user
            if (processedUsers.indexOf(userSysId) !== -1) {
                continue;
            }
            
            processedUsers.push(userSysId);
            
            // Get user email
            var userGr = new GlideRecord('sys_user');
            if (userGr.get(userSysId)) {
                var email = userGr.getValue('email');
                if (email && uniqueEmails.indexOf(email) === -1) {
                    uniqueEmails.push(email);
                }
            }
        }
        
        // Set flow variable with unique emails
        fd_data.setValue('uniqueEmailList', uniqueEmails.join(','));
        fd_data.setValue('userCount', uniqueEmails.length);
        
        gs.info('Unique emails collected: ' + uniqueEmails.length);
        
    } catch (error) {
        gs.error('Error collecting unique emails: ' + error.message);
        fd_data.setValue('uniqueEmailList', '');
    }
})();

Step 2: Create Flow Variables

  • uniqueEmailList (String)
  • userCount (Number)

Step 3: Add Send Email Action

  • To: {{uniqueEmailList}}
  • Subject: Your notification subject
  • Body: Your message content

Mark it ashelpful if this helps you to understand. Accept solution if this give you the answer you're looking for.
Regards,
Venkat.