The Zurich release has arrived! Interested in new features and functionalities? Click here for more

add a group to cc of email notifications

NiloferS
Tera Contributor

NiloferS_0-1718363403332.png

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. 

4 REPLIES 4

Abhishek_Thakur
Mega Sage

Hello @NiloferS ,

 

In line no 17 use getValue() instead of using get.

Yashsvi
Kilo Sage

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. 

Jitendra Diwak1
Kilo Sage

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 

Please accept my solution if it works for and thumps up.

kuldeeeep
Tera Contributor

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}