Email notification to list collector variable

Joshuu
Kilo Sage

Hi All,

 

I have a variable on the catalog form as list collector which is reference to group table. We need to send a notification to the group members which are selected on the form in this list collector variable. I have created a notification activity in the workflow and in "To(script)" field I have added below script. But it is not working for multiple groups. Not sending notification for multiple groups selected on the form.

 

Could you please help me on this?

 

 

// Get the group Sys ID from the list collector variable
var groupSysId = current.variables.notification;

var recipients = ''; // Initialize the recipients string

if (groupSysId) {
    // Query the sys_user_grmember table to find all users in the group
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group', groupSysId);
    gr.query();
    var emails = [];

    while (gr.next()) {
        // Add the email of each group member to the array
        if (gr.user.email) {
            emails.push(gr.user.email.toString());
        }
    }

    // Join the emails array into a comma-separated string
    recipients = emails.join(',');
}

// Return the recipients for the workflow notification
recipients;

 

 

1 ACCEPTED SOLUTION

Lithesh
Kilo Sage

 

Hi @Joshuu ,

Find the commented line for your understanding

var groupSysId = current.variables.notification;

var recipients = ''; // Initialize the recipients string

if (groupSysId) {
    // Query the sys_user_grmember table to find all users in the group
    var gr = new GlideRecord('sys_user_grmember');
   // gr.addQuery('group', groupSysId); //Wrong query since it has multiple sysid if you query like this it will only get one value.
gr.addEncodedQuery('group.sys_idIN'+groupSysId); // this will return all the user in the multiple groups
    gr.query();
    var emails = [];

    while (gr.next()) {
        // Add the email of each group member to the array
        if (gr.user.email) {
            emails.push(gr.user.email.toString());
        }
    }

    // Join the emails array into a comma-separated string
    recipients = emails.join(',');
}

// Return the recipients for the workflow notification
recipients;

If you feel it is helpful mark it as correct,

 

Thanks,

Lithesh

View solution in original post

4 REPLIES 4

Shruti
Mega Sage
Mega Sage
var groupSysId = current.variables.notification;

var recipients = ''; // Initialize the recipients string

if (groupSysId) {
    // Query the sys_user_grmember table to find all users in the group
    var gr = new GlideRecord('sys_user_grmember');
    gr.addEncodedQuery('group.sys_id'+groupSysId);
    gr.query();
    var emails = [];

    while (gr.next()) {
        // Add the email of each group member to the array
        if (gr.user.email) {
            emails.push(gr.user.email.toString());
        }
    }

    // Join the emails array into a comma-separated string
    recipients = emails.join(',');
}

// Return the recipients for the workflow notification
recipients;

Lithesh
Kilo Sage

 

Hi @Joshuu ,

Find the commented line for your understanding

var groupSysId = current.variables.notification;

var recipients = ''; // Initialize the recipients string

if (groupSysId) {
    // Query the sys_user_grmember table to find all users in the group
    var gr = new GlideRecord('sys_user_grmember');
   // gr.addQuery('group', groupSysId); //Wrong query since it has multiple sysid if you query like this it will only get one value.
gr.addEncodedQuery('group.sys_idIN'+groupSysId); // this will return all the user in the multiple groups
    gr.query();
    var emails = [];

    while (gr.next()) {
        // Add the email of each group member to the array
        if (gr.user.email) {
            emails.push(gr.user.email.toString());
        }
    }

    // Join the emails array into a comma-separated string
    recipients = emails.join(',');
}

// Return the recipients for the workflow notification
recipients;

If you feel it is helpful mark it as correct,

 

Thanks,

Lithesh

Tai Vu
Kilo Patron
Kilo Patron

Hi @Joshuu 

The script inside the Notification Workflow Activity accepts a comma-separated list of user or group sys_ids. You can just return the variables.notification list collector that contains the comma-separated list of groups that need to be notified.

// Set the variable 'answer' to a comma-separated list of user or group sys_ids that you want the email sent to.
 
Just considering Event Registry to trigger notifications in this scenario. Embedding notifications directly inside workflows can limit flexibility and reusability. It can also be time-consuming and prone to errors when changes to notification content are needed.
 
Cheers,
Tai Vu 

Moin Kazi
Kilo Sage
Kilo Sage

Hi @Joshuu ,

 

If the notification is a list collector field referring to the sys_user_group table, you should use the script below.

 

// Get the group Sys ID from the list collector variable 
var groupSysId = current.variables.notification;

var recipients = ''; // Initialize the recipients string

if (groupSysId) {
    // Query the sys_user_grmember table to find all users in the group
    var gr = new GlideRecord('sys_user_grmember');
    gr.addEncodedQuery('group.sys_idIN'+groupSysId);
    gr.query();
    var emails = [];

    while (gr.next()) {
        // Get the email of each group member
        var email = gr.user.getValue('email');
        if (email && !emails.includes(email)) { // Check for duplicates
            emails.push(email); // Add only if it's not already in the array
        }
    }

    // Join the emails array into a comma-separated string
    recipients = emails.join(',');
}

// Return the recipients for the workflow notification
recipients;

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.

 

Thank you!
Moin Kazi

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~