add a user/group in CC of an email

NiloferS
Tera Contributor

I have a custom table and the form as below:

NiloferS_0-1718355405559.png

We want Recipient in cc of email notification. I have written a email script as below:

NiloferS_1-1718355581332.png

NiloferS_2-1718355839398.png

Its not working as expected.

The email template has nothing mentioned in it, so its not overriding my code.

Note: this email notification is not Global. It is restricted a particular application

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@NiloferS 

your email script should be like this

(function runMailScript(current, template, email, email_action, event) {
	
	email.addAddress('cc',current.recipient.email.toString(),current.recipient.name.toString());

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

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

Anand Kumar P
Giga Patron
Giga Patron

Hi @NiloferS ,
If you have single recipient you can try script suggested by Ankur if you have multiple recipients try below script in email script

 

if(!current.recipient.nil()){
   var watcherIds = current.recipient.split(",");
   var user =new GlideRecord("sys_user");
   user.addQuery("sys_id", watcherIds);
   user.addQuery("email","!=","");
   user.query();
   while(user.next()){
      email.addAddress("cc", user.email, user.getDisplayValue());
}
}

 

  In Notification: ${mail_script:your_mailscript_name}

 

Thanks,

Anand

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@NiloferS 

your email script should be like this

(function runMailScript(current, template, email, email_action, event) {
	
	email.addAddress('cc',current.recipient.email.toString(),current.recipient.name.toString());

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

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Anand Kumar P
Giga Patron
Giga Patron

Hi @NiloferS ,
If you have single recipient you can try script suggested by Ankur if you have multiple recipients try below script in email script

 

if(!current.recipient.nil()){
   var watcherIds = current.recipient.split(",");
   var user =new GlideRecord("sys_user");
   user.addQuery("sys_id", watcherIds);
   user.addQuery("email","!=","");
   user.query();
   while(user.next()){
      email.addAddress("cc", user.email, user.getDisplayValue());
}
}

 

  In Notification: ${mail_script:your_mailscript_name}

 

Thanks,

Anand

Yashsvi
Kilo Sage

Hi @NiloferS,

please check below links:

 

https://www.youtube.com/watch?v=_yZ3ZOVf05Y

https://www.youtube.com/watch?v=2k7szQymVBM

 

Thank you, please make helpful if you accept the solution.

Reach out us on +91 6304422358 for complete training!! Explore SERVICENOW UDEMY COURSES -- YOUR RATING IS TRULY APPRECIATED!! SERVICENOW SCRIPTING COURSE: https://www.udemy.com/course/servicenow-scripting-mastery-advanced-development/ SERVICENOW ADMIN COURSE: ...
To add email addresses to the cc or bcc field of an outbound email in ServiceNow, you can use a mail script and the email.addAddress method. To call the script on the notification, append ${mail_script:xxx}. You can add multiple email addresses by iterating through a while loop in the script. To ...

Service_RNow
Mega Sage

Hi @NiloferS 

-> You need to create a email script and define the user you want to set as cc in email and then call the email script from the notification.

Steps to create Email Script and how to use:

  1. Navigate to the "Notification email Script" in the "System Notification" in filter navigator.
  2. Set the name of email notification "add_newusers_to_cc".
  3. add below code in the script:
 email.addAddress("cc","email_address_of_the_user","Name_of_ther_user");
//if you want to push the user dynamically then you can use the scripting to validate and push the user.

4. Now navigate to the notification where you want to add cc.

5. add "${mail_script:add_newusers_to_cc}" in "Message HTML" field in "What it will contain" form section of notification.

Kindly mark my answer as Correct and helpful based on the Impact.