logo

NJP

Scripting with Email Client Templates

Import · Dec 19, 2018 · article

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!

View original source

https://www.servicenow.com/community/developer-articles/scripting-with-email-client-templates/ta-p/2330257