How to add a group in "CC" in an Email Notification?

Not applicable

Hi,

 

I have one requirement: I need to add a group to the 'CC' in an email notification using an email script.

 

Note that - this group doesn't have an email ID, so I need to send the email to the individual members of the group.

 

For example, if there is a group called 'Global' with five members, I need to add those five members to the "CC" 

14 REPLIES 14

Not applicable

You may try using within the script:

email.setCc('abc@xyz.com', 'pqr@xyz.com');

 

SP22
Giga Sage

Hello @Community Alums ,

Can you please try this email script and let me know whether it is useful or not.

// Get the group from the current record dynamically (e.g., assigned group on an incident or task)

var groupSysId = current.assigned_group;  // Replace 'assigned_group' with the correct reference field

if (groupSysId) {  // Check if the group field is not empty

    var group = new GlideRecord('sys_user_group');

    if (group.get(groupSysId)) {

        // Loop through all the members of the group

        var groupMembers = new GlideRecord('sys_user_grmember');

        groupMembers.addQuery('group', group.sys_id);

        groupMembers.query();

        

        // Check if there are members in the group

        if (groupMembers.hasNext()) {

            while (groupMembers.next()) {

                // Dynamically print each member's email

template.print(groupMembers.user.email + ', ');

            }

        } else {

gs.log('No members found in the group: ' + group.name);

        }

    } else {

        gs.log('Group not found with sys_id: ' + groupSysId);

    }

} else {

    gs.log('No group is assigned to this record.');

}

 
Thanks
SP.

 

Rohit99
Mega Sage

Hi @Community Alums,


You may try with the following mail script.

Script :

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group', 'd3afa0d883301210d3ebb755eeaad311'); //sys_id of specific group
    gr.query();
    while (gr.next()) {
        email.addAddress("cc",gr.user.email,gr.group.getDisplayValue());
    }
})(current, template, email, email_action, event);

and in Notification Body just add:
${mail_script:your_mail_script_name} 


Rohit99_0-1726659669935.png

 

Rohit99_1-1726659704583.png

 

Rohit99_2-1726659748710.png

 

 

Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi

 

Ayushi12
Mega Sage

Hi @Community Alums,
You can write the email script and call email script in notification.
Refer below code:
In Notification Body add - ${mail_script:<Your script name>} 
e.g: ${mail_script:test_email_script}  
Email Script - 

    // Add your code here
    var grpemail= new GlideRecord('sys_user_grmember');
    grpemail.addQuery('group', '287ee6fea9fe198100ada7950d0b1b73'); // Replace with sys_id of the required group
    grpemail.query();
    while (grpemail.next()) {
         email.addAddress("cc",grpemail.user.email.toString(),grpemail.user.name.toString());
    }

If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

Thanks!

 

AmolJ
Tera Expert

Just add the sys_id of the group to either "To" or "CC" fields of the "Send Email" action in the flow. It sends emails to all group members including the group manager (in absence of group email address)