Unable to fetch Email Template
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2024 08:40 PM
Can someone please help?
Widget Editor's Function for triggering script include :
c.sendEmail = function() {
Script Include :
var GetEmailTemplate = Class.create();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2024 10:18 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2024 01:00 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2024 08:15 AM - edited ‎11-13-2024 08:22 AM
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)
});