add a group to cc of email notifications
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 04:12 AM
written a email script to add a Group (which has one user as of now) to cc of a notification. its not working as expected.
Kindly help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 04:15 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 04:18 AM
Hi @NiloferS,
use this script:
(function executeRule(current, previous) {
// Create a new GlideEmailOutbound object
var email = new GlideEmailOutbound();
// Define recipient email address (replace with actual or dynamic value)
var recipientEmail = current.getValue('email_field_name'); // Replace with your field name or recipient's email address
email.addRecipient(recipientEmail);
// Define CC email address for a group (replace with actual or dynamic value)
var groupEmail = gs.getProperty('group_cc_email'); // Replace with your property name or method to retrieve the group's email
email.addCC(groupEmail);
// Set email subject and body
email.setSubject('Notification from ServiceNow');
email.setBody('This is a notification email.');
// Send the email
email.send();
})(current, previous);
Thank you, please make helpful if you accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 04:34 AM
Hi @NiloferS,
Please store the value of line no 17 in a variable. Variable should be created outside of the loop. Your user is while scope and it stores the data inside while loop and you are not able to use outside. Declare variable at line 7 and store the value of line 17 in it then use it.
Please accept my solution if it resolves your issue and thumps 👍 up
Thanks
Jitendra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2024 01:22 AM
Here is the corrected code, it will add group members to CC field.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
var groupName = 'TestGroup';
var group = new GlideRecord('sys_user_group');
group.addQuery('name', groupName);
group.query();
if (group.next()) {
gs.log('Group found: ' + group.name);
var groupMembers = new GlideRecord('sys_user_grmember');
groupMembers.addQuery('group', group.sys_id);
groupMembers.query();
while (groupMembers.next()) {
var user = new GlideRecord('sys_user');
user.get(groupMembers.user); // Correct method to get user record
gs.log('User found: ' + user.name + ', Email: ' + user.email); // Log user details for debugging
email.addAddress('cc', user.email, user.name); // Ensure email object is defined
}
} else {
gs.log('Group not found: ' + groupName);
}
})(current, template, email, email_action, event);
You can ignore the gs.log lines, it was used to get the output
I have tested with with a group name "TestGroup" that has 4 members
Upon calling the mailscript in notification email body it added members to CC (copied to)
i.e. ${mail_script:your_mail_script_name}