Variables not producing value on template set by email script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2023 02:43 AM
I've created an application in ServiceNow application studio for sending emails to the users on their birthdays and anniversaries. Everything is working fine except email templates. when I am selecting the email template manually in the notification then the value of variable is getting printed to email body, but when I am setting the template dynamically through email script then the variable is printed on the email body as it is eg: "${first_name}" .
Script Include:
var OccasionEmailSender = Class.create();
OccasionEmailSender.prototype = {
initialize: function() {},
// Function to send birthday email to a user
sendRandomBirthdayEmail: function() {
var gdt = new GlideDateTime();
var day = gdt.getDayOfMonth();
var month = gdt.getMonth();
var year = gdt.getYear();
// Query users having birthday today!
var user = new GlideRecord('sys_user');
// user.addEncodedQuery("u_date_of_birthDATEPARTSeptember@javascript:gs.datePart('month','month','EE')");
user,addEncodedQuery("u_date_of_birthDATEPARTMay@javascript:gs.datePart('month','month','EE')");
// u_date_of_birthDATEPARTMay name of query
user.addQuery('u_date_of_birth', 'ENDSWITH', day);
user.addActiveQuery();
user.query();
while (user.next()) {
gs.info('Very Happy Birthday ' + user.first_name);
// Query user details based on userId
var userEmail = user.email;
var userName = user.name;
var firstName = user.first_name;
// Triger the Event for birthday
gs.eventQueue('birthday.email.send', user, userEmail, firstName);
// Log the email sent
gs.info("Birthday email sent to " + userEmail + " on " + userName);
}
},
sendRandomAnniversaryEmail: function() {
// Query users having Work Anniversary today!
var user = new GlideRecord('sys_user');
user.addActiveQuery();
user.addQuery('u_date_of_joining', 'ENDSWITH', day);
user.addEncodedQuery("u_date_of_joiningDATEPARTOctober@javascript:gs.datePart('month','month','EE')");
user.query();
while (user.next()) {
//Finding total years of work for user
var doj = new GlideDateTime(user.getValue('u_date_of_joining'));
var joining_year = doj.getYear();
var totalYears = year - joining_year;
// Query user details based on userId
userEmail = user.email;
userName = user.name;
gs.info('Very Happy Anniversary ' + user.first_name + "\nYou've successfully completed " + totalYears + " years with our company!");
// Triger the Event for birthday
gs.eventQueue('anniversary.email.send', user, userEmail, totalYears);
// Log the email sent
gs.info("Work Anniversary email sent to " + userEmail + " " + userName);
}
},
type: 'OccasionEmailSender'
};
Events:
Notification:
Notification Email Script:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var gr = new GlideRecord('sysevent_email_template');
gr.addEncodedQuery('nameSTARTSWITHbirthday');
gr.query();
if (gr.hasNext()) {
var templateCount = gr.getRowCount();
var randomIndex = Math.floor(Math.random() * templateCount);
gr.first();
// Iterate to the random index in the result set
for (var i = 0; i <= randomIndex; i++) {
gr.next();
}
// var temp = gr;
// gs.info('Message HTML: ' + temp.name);
// var notification = new GlideRecord('sysevent_email_action');
// notification.addEncodedQuery('active=true^nameLIKEbirthday');
// notification.query();
// if (notification.next()) {
// notification.setValue('template', temp.sys_id);
// notification.update();
// gs.info("Template Changed To: " + notification.getDisplayValue('template'));
// }
template.print(gr.message_html);
} else {
gs.info('No records with a defined message_html field were found.');
}
})(current, template, email, email_action, event);
Template:
Email Layout:
The "${first_name}" in the email layout is printing value to the email body. But "${first_name}" from template message_html is not printing the value but it's printing "${first_name}" as a string. I am attaching the preview of the email:
Here "Dear Abel" is coming from email layout and "First Name: ${first_name}" is coming from template. In template the variable is not printing the value to the email body. If anyone has faced this issue or know the solution for this, your help will be much appreciated.
Thanks in advance,