- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 01:26 AM - edited 10-22-2024 01:40 AM
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;
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 01:45 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 01:39 AM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 01:45 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 01:57 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 02:12 AM - edited 10-22-2024 02:15 AM
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~