Unable to fetch Email Template

Samyak613
Tera Contributor
I am facing an issue with email template, I am able to open the email outlook draft but the email template content is not fetched and returns null. I have set it up as when a user clicks on a button it triggers a function which calls the correct email template based on a field's content which is Illegal Activations or Double Activations. 

Can someone please help? 

Widget Editor's Function for triggering script include : 

c.sendEmail = function() {
    var ga = new GlideAjax('GetEmailTemplate');
    ga.addParam('sysparm_name', 'getEmailBody');
    ga.addParam('sysparm_subject', c.subject);
    ga.addParam('sysparm_greetingName', c.greetingName);
 
    ga.getXMLAnswer(function(response) {
console.log('Server response:', response);
        var templateContent = response;
        var encodedTo = encodeURIComponent(c.to);
        var encodedCc = encodeURIComponent(c.cc);
        var encodedSubject = encodeURIComponent(c.subject);
        var encodedBody = encodeURIComponent(templateContent);
 
        var mailToLink = "mailto:" + encodedTo + "?cc=" + encodedCc + "&subject=" + encodedSubject + "&body=" + encodedBody;
 
        window.location.href = mailToLink;
    });


Script Include : 

var
 GetEmailTemplate = Class.create();
GetEmailTemplate.prototype = {
    initialize: function() {},

 

    getEmailBody: function() {
        var subject = this.getParameter('sysparm_subject');
        var greetingName = this.getParameter('sysparm_greetingName');
        var version = this.getParameter('sysparm_version') || 'none';

 

        var templateName = (subject === "Illegal Activations") ?
            "Illegal Activations Email Template" : "Double Activations Email Template";

 

        var template = new GlideRecord('sysevent_email_template');
        template.get('name', templateName);

 

        var body = template.getValue('body');
 
        body = body.replace('${greeting_name}', greetingName);

 

        return body;
    },

 

    type: 'GetEmailTemplate'
};
3 REPLIES 3

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @Samyak613 - Adding some logging might be helpful, but records in sysevent_email_template don't have a 'body' field, so with var body = template.getValue('body');. body will always return null.

Then the message field can we fetch it using 
var message = template.getValue('Message'); 

I tried this but it is still returning null. How can we call the content in script include? 

There are some other issues with your code...

 

var version = this.getParameter('sysparm_version') || 'none';

 

You never pass this parameter, and version appears to be an unused variable.

 

var subject = this.getParameter('sysparm_subject');
...
var templateName = (subject === "Illegal Activations") ? "Illegal Activations Email Template" : "Double Activations Email Template";

 

this.getParameter('sysparm_subject') returns an object, not a simple string. Because the === operator checks both value and type, and subject is an object, it can never be strictly equal (===) to a string, even if it contains text that appears similar. You need to convert subject to a string before doing the comparison.

 

var template = new GlideRecord('sysevent_email_template');
template.get('name', templateName);

 

You should always check that the .get() method actually returned a GlideRecord.

 

Try something like this:

 

var GetEmailTemplate = Class.create();
GetEmailTemplate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getEmailBody: function() {
        var subject = this.getParameter('sysparm_subject');
        var greetingName = this.getParameter('sysparm_greetingName') || '';
        var templateName = (subject.toString() === "Illegal Activations") ?
            "Illegal Activations Email Template" : "Double Activations Email Template";

        var template = new GlideRecord('sysevent_email_template');

        // Return if template is not found
        if (!template.get('name', templateName)) {
            return 'Template not found';
        }

        // Get the message and perform replacement only if message is not empty
        var message = template.getValue('message');
        return message ? message.replace('${greeting_name}', greetingName) : '';
    }
});

 

Make sure the Client callable is checked.

 

Client code would be something like this, answer will be the message (or "Template not found").

 

var ga = new GlideAjax('GetEmailTemplate');
ga.addParam('sysparm_name', 'getEmailBody');
ga.addParam('sysparm_subject', c.subject);
ga.addParam('sysparm_greetingName', c.greetingName);
ga.getXMLAnswer(function(answer) {
    // Callback function here (do something with the answer)
});