Trevor Muhl
Kilo Sage

One of our business requirements includes customization of "From" and "Reply To" email addresses for all customer-facing outbound emails. While I understood how to achieve this within notification records directly or by using email scripts, I had greater trouble doing so for the email client feature.

I discovered ServiceNow documentation describing the basic process for customizing email client templates here:

Set the from address with an email client template

The document above recommends the following advanced script:

javascript:gs.getUserDisplayName()+" <"+ gs.getUser().getEmail()+">"

Unfortunately, I quickly found several shortcomings with these client templates.

I have stored the desired email address and display names within system properties to make updating these values later hassle-free. I have to reference these properties from all features to increase maintainability of my application.

Whenever I attempted to access a system property within an email client template, it would return "undefined", regardless of the property's scope or any applied read restrictions. To make matters worse, all script includes in my scoped application were also inaccessible. Neither of the following attempts worked as I had hoped:

javascript:gs.getProperty('x_code.my_email.display_name')+" <"+ gs.getProperty('x_code.my_email.address')+">"
javascript:new MyEmailUtil().getFullAddress()

After some further testing, I found that other global script includes were accessible! This was the breakthrough I needed. For whatever reason, client templates within a scoped application cannot access artifacts within the same scope. I simply needed to access those properties via a globally-accessible script include within my application.

I created the following script include:

Name: MyEmailUtil

Client callable: true

Accessible from: All application scopes

Script:

var MyEmailUtil = Class.create();

MyEmailUtil.display_name = gs.getProperty('x_code.my_email.display_name');
MyEmailUtil.address = gs.getProperty('x_code.my_email.address');

MyEmailUtil.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	getEmailUtil: function() {
		return JSON.encode(MyEmailUtil);
	},
	
	getFullAddress: function() {
		return MyEmailUtil.display_name + ' <' + MyEmailUtil.address + '>';
	},

    type: 'MyEmailUtil'
});

This script include is client accessible to allow retrieval of those values within client scripts or other client-side artifacts via Ajax. This is not necessary for this effort, but may certainly be helpful for other efforts!

Now, within the "From" and/or "Reply to" fields within the client template, we can set the following:

javascript: new x_code.EmailUtil().getFullAddress()

The following statements also work to access those individual properties:

EmailUtil.display_name
EmailUtil.address

The email client now sets those fields exactly as I wish! I realize this solution is fairly niche, but hope it is helpful, regardless. Let me know if you have any questions or better solutions. Thank you!

Comments
Shengavula Rath
Tera Expert

@Trevor Muhl 

trying to use script include in the BCC of Email Client Template.

 

I am trying to autopopulate the some certain email addresses in Bcc Field of email client Template. For this I have written a script include and calling that script include in the bcc field as shown below. But this script include is not executing for some reason.

I am calling script include on bcc as below: 

javascrip: new AffectedCISubscribers().AffectedCI();

 

Script Include: (all application scopes)

var AffectedCISubscribers = Class.create();
AffectedCISubscribers.prototype = Object.extendsObject(AbstractAjaxProcessor, {

AffectedCI: function() {
var afc = new GlideRecord("task_ci");
afc.addQuery('task', current.sys_id);
afc.query();
var inc1 = [];
while (afc.next()) {
var inc2 = afc.getValue('ci_item');
inc1.push(inc2);
}

var nof = new GlideRecord("sys_notif_subscription");
nof.addQuery('affected_record', inc1);
nof.query();
var xyz = [];
while (nof.next()) {
xyz.push(nof.user.email);
}
return xyz;
},
type: 'AffectedCISubscribers'
});

 

I also tried changing the name of the function same as the script include name and then in Bcc I wrote this way

javascript&colon; AffectedCISubscribers(); 

but even this didn't work.

 

Please let me know what changes I need to do to make this work.

Trevor Muhl
Kilo Sage

@Shengavula Rath 

There are two things that you should try:

  1. Use the scope name when calling your script include. Example: "x_xyz.AffectedCISubscribers.AffectedCI();". If it is a global script include, use "global.AffectedCI...".
  2. Try returning a string instead of an array in your script. The email client requires a semicolon-separated list of email addresses. You are currently returning an array of GlideElements. Try using ".toString()" on line 19 and ".join(';')" on line 21.
Version history
Last update:
‎12-19-2018 11:25 AM
Updated by: