Trying to add CC to a email notification through email script.

NiloferS
Tera Contributor

I have written a email script with below code. 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
var cRecord=email.current;
var ccEmail=cRecord.on_behalf_email;
if(ccEmail){
email.addAddress('cc',ccEmail);
}
})(current, template, email, email_action, event);
 
This code suppose to put the email address from a field called "on_behalf_email" from the table "u_test". I have called the above mail script in notification body as below
NiloferS_0-1717434552161.png

This is not working as expected. What could possibly went wrong?

4 ACCEPTED SOLUTIONS

johnfeist
Mega Sage
Mega Sage

Hi NiloferS,

I can suggest a few things:

  • The documentation shows email.addAddress() with three arguments.  The third one seems to be optional, but you never know.
  • Are you sure that you have the mail script name exactly the same as it exists for that object.  Remember this is the Linux world so case matters.
  • Have you tried embedding diagnostics (gs.log) in the script to see if it is getting called or if there is some other error?  Along with that, when you test, have you checked for errors or warnings?

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

View solution in original post

James Chun
Kilo Patron

Hi @NiloferS,

 

I am assuming the notification table is configured on the [u_table].

Within the script, replace the first line of code with var cRecord = current;

 

Also, addAddress takes 3 parameters which might be all mandatory.

JamesChun_1-1717445052031.png

 

JamesChun_0-1717445040307.png

 

Cheers

 

 

Cheers

View solution in original post

Community Alums
Not applicable

Hi @NiloferS ,

Can you please try the below code-

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

    var cRecord = current;
    var ccEmail = cRecord.getValue('on_behalf_email');
	if (ccEmail) {
        email.addAddress('cc', ccEmail);
    }

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

 

Take a look into the below documentation as well.
https://docs.servicenow.com/bundle/vancouver-platform-administration/page/script/server-scripting/re...

 

As James suggested you can take a look into the below script along with the explanation on Docs site-

SanjayG_0-1717458135591.png

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

View solution in original post

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Regarding "This is not working as expected. What could possibly went wrong?"

 

How are you testing this? By previewing messages? Are actually sending out emails and verifying those? Good to know, CC / BCC doesn't work "correctly" when previewing, though does work when actually sending emails.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

4 REPLIES 4

johnfeist
Mega Sage
Mega Sage

Hi NiloferS,

I can suggest a few things:

  • The documentation shows email.addAddress() with three arguments.  The third one seems to be optional, but you never know.
  • Are you sure that you have the mail script name exactly the same as it exists for that object.  Remember this is the Linux world so case matters.
  • Have you tried embedding diagnostics (gs.log) in the script to see if it is getting called or if there is some other error?  Along with that, when you test, have you checked for errors or warnings?

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

James Chun
Kilo Patron

Hi @NiloferS,

 

I am assuming the notification table is configured on the [u_table].

Within the script, replace the first line of code with var cRecord = current;

 

Also, addAddress takes 3 parameters which might be all mandatory.

JamesChun_1-1717445052031.png

 

JamesChun_0-1717445040307.png

 

Cheers

 

 

Cheers

Community Alums
Not applicable

Hi @NiloferS ,

Can you please try the below code-

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

    var cRecord = current;
    var ccEmail = cRecord.getValue('on_behalf_email');
	if (ccEmail) {
        email.addAddress('cc', ccEmail);
    }

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

 

Take a look into the below documentation as well.
https://docs.servicenow.com/bundle/vancouver-platform-administration/page/script/server-scripting/re...

 

As James suggested you can take a look into the below script along with the explanation on Docs site-

SanjayG_0-1717458135591.png

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Regarding "This is not working as expected. What could possibly went wrong?"

 

How are you testing this? By previewing messages? Are actually sending out emails and verifying those? Good to know, CC / BCC doesn't work "correctly" when previewing, though does work when actually sending emails.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn