Script help with Notification email script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 12:57 PM
Hi Team, I have created a notification email script however its not working as expected and not showing an variables data on the notification.
Notification: Health status update
Message html: ${mail_script:health_status_variables}
Email Script: health_status_variables
Script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 01:44 PM
Hi @dvelloriy if you are trying to add variable to email please try below
(function runMailScript(current, template, email, email_action, event) {
// Add your code here
template.print('Variables: <br/>');
var variables = current.variables.getElements();
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
var label = question.getLabel();
var value = question.getDisplayValue();
if(label != ''){
template.space(4);
template.print(' ' + label + " = " + value + "<br/>");
}
}
})(current, template, email, email_action, event);
Please mark my answer correct and helpful if this works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 11:57 PM
- The loop should start at 0, not 1, as arrays in JavaScript are zero-indexed.
- It's good practice to ensure that the object exists before processing it in a loop.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {var util = new global.GlobalServiceCatalogUtil();
var obj = util.getVariablesForTask(current);// Ensure the object exists and has variables before looping
if (obj && obj.length) {
for (var i = 0; i < obj.length; i++) { // Starting the loop from 0, as arrays are zero-indexed
if (obj[i].label !== 'Form State') { // Avoid printing the 'Form State'
template.print(obj[i].label + ' : ' + obj[i].display_value + '<br>');
}
}
}})(current, template, email, email_action, event);
mark my answer correct and helpful if this works for you.