Add User in CC of an email

may13
Tera Contributor
Hello,
I have a requirement in my current project where I need to create an email script in ServiceNow. In the User table, I have three custom fields: L1 Approval, L2 Approval, and HR Employee. I need to include the users from these fields in the CC section of a notification. Could you please provide the email script?

 

6 REPLIES 6

Ankur Bawiskar
Tera Patron

@may13 

since your notification is on HR Profile use this

-> you need to dot walk to user field and then get the details

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    email.addAddress('cc', current.user.L1_approvalField.email.toString(), current.user.L1_approvalField.getDisplayValue());

    email.addAddress('cc', current.user.L2_approvalField.email.toString(), current.user.L2_approvalField.getDisplayValue());

    email.addAddress('cc', current.user.HR_EmployeeField.email.toString(), current.user.HR_EmployeeField.getDisplayValue());


})(current, template, email, email_action, event);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Its_Sagnic
Mega Guru

Hi @may13 ,

Hope you are doing well.

You need to create a email script first and need to add it in the Email notification to make it work.

So, please follow the steps below to make it work .

Step - 1 : 
1. go to system notification > email > Notification Email Script.
2. Create a new script named : email_CC.

Code :

(function runMailScript(current, template, email, email_action, event) {
    // Helper function to safely add CC
    function addCC(field) {
        if (field && field.email) {
            email.addAddress('cc', field.email.toString(), field.getDisplayValue());
        }
    }

    // Attempt to add CC for each field
    addCC(current.user.L1_approvalField);
    addCC(current.user.L2_approvalField);
    addCC(current.user.HR_EmployeeField);

})(current, template, email, email_action, event);

 Step - 2  :

How to Use It in an Email :
  1. Open the Notification: Navigate to System Notification > Email > Notifications and select your desired notification record.
  2. Insert the Script Call: In the Message HTML field (within the What it will say tab), place the following line anywhere in the body:
    • ${mail_script:email_cc}
  3. Verify Configuration: Ensure the Advanced view is enabled on the notification so the system properly processes the mail script during the email generation phase. 

If my response has helped you, kindly mark it as helpful and accept the solution.

Regards,

Sagnic