- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 01:26 AM
Hi all. I recently found this excellent Email Script that displays all Requested Item variables in a Notification if they are not empty:
(function runMailScript(current, template, email, email_action, event) {
// Get Requested Item
var reqitem = current.request_item;
// Get Owned Variables for Requested Item and sort by Order
var ownvar = new GlideRecord('sc_item_option_mtom');
ownvar.addQuery('request_item.number', reqitem.number);
ownvar.addQuery('sc_item_option.value', '!=', '');
ownvar.orderBy('sc_item_option.order');
ownvar.query();
while (ownvar.next()) {
// Add Question, Answer and Order into notification mail
// Set variable v to variable name
var field = ownvar.sc_item_option.item_option_new;
var fieldValue = ownvar.sc_item_option.item_option_new.name;
// Print variable name and Display Value for each variable in Requested Item
template.print('<p><b>' + field.getDisplayValue() + '</b>' + ': ' + reqitem.variables[fieldValue].getDisplayValue()) + '</p>';
}
})(current, template, email, email_action, event);
However, my requirement is to achieve the same thing, but only have the Notification display the variables on a Catalog Task. As you know, from the Workflow Editor, you can specify which variables from a RITM you want to display in a Catalog Task - these are the ones I want to extract.
Any thoughts?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 05:31 AM
Hi
Just change your script to this
(function runMailScript(current, template, email, email_action, event) {
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.request_item);
item.query();
while (item.next()) {
var keys = new Array();
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(item.sys_id);
set.setTaskID(current.sys_id);
set.load();
var vs = set.getFlatQuestions();
if(vs.size() == '0' || vs.size() == '')
{
return;
}
else
{
template.print("<hr style='width: 98%;' /><p><b><u>Task Details</u></b></p>");
for (var i = 0; i < vs.size(); i++) {
if (vs.getLabel() != "" && vs.getDisplayValue() != "" && vs.getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != '') {
template.print("<p><b>" + vs.get(i).getLabel() + "</b>: " + vs.get(i).getDisplayValue() + "</p>");
}
}
}
}
})(current, template, email, email_action, event);
Sorry script not tested but try it once 🙂
Thank you
Prasad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 01:34 AM
Hi
See if this helps
https://community.servicenow.com/community?id=community_question&sys_id=2f5ff2a9db58dbc01dcaf3231f961935
Thank you
Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 04:39 AM
Thanks Prasad. I switched out the code and it works just as intended.
Question though. How do I get it to ignore the script if there are no variables? Here is what is happening when I preview the notification with a Catalog Task with no variables:
Ideally it shouldn't print the horizonal line and the Task Details unless there are any variables. The script I'm using now:
(function runMailScript(current, template, email, email_action, event) {
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.request_item);
item.query();
while (item.next()) {
template.print("<hr style='width: 98%;' /><p><b><u>Task Details</u></b></p>");
var keys = new Array();
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(item.sys_id);
set.setTaskID(current.sys_id);
set.load();
var vs = set.getFlatQuestions();
for (var i = 0; i < vs.size(); i++) {
if (vs.getLabel() != "" && vs.getDisplayValue() != "" && vs.getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != '') {
template.print("<p><b>" + vs.get(i).getLabel() + "</b>: " + vs.get(i).getDisplayValue() + "</p>");
}
}
}
})(current, template, email, email_action, event);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 05:31 AM
Hi
Just change your script to this
(function runMailScript(current, template, email, email_action, event) {
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.request_item);
item.query();
while (item.next()) {
var keys = new Array();
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(item.sys_id);
set.setTaskID(current.sys_id);
set.load();
var vs = set.getFlatQuestions();
if(vs.size() == '0' || vs.size() == '')
{
return;
}
else
{
template.print("<hr style='width: 98%;' /><p><b><u>Task Details</u></b></p>");
for (var i = 0; i < vs.size(); i++) {
if (vs.getLabel() != "" && vs.getDisplayValue() != "" && vs.getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != '') {
template.print("<p><b>" + vs.get(i).getLabel() + "</b>: " + vs.get(i).getDisplayValue() + "</p>");
}
}
}
}
})(current, template, email, email_action, event);
Sorry script not tested but try it once 🙂
Thank you
Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 07:50 AM
Brilliant, thanks Prasad. I didn't realise getFlatQuestions was getting the variables