- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 12:31 PM
I want to list all the Catalog Item variables in the Notification activity of the Workflow. There is no Catalog Item being created instead mail will be sent out with all Catalog Item variable and their values.
I found the following code as a solution: https://www.servicenow.com/community/developer-forum/all-catalog-task-variables-displayed-in-a-notif...
(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);
But it fails at:
var reqitem = current.request_item;
If I print reqitem in gs.info() it shows undefined.
Same issue with the following code:
(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);
Fails at:
item.addQuery("sys_id", current.request_item);
Following is a screenshot of the notification activity. Using Mail Script and regular Run Script activity, but the email body does not contain variable details.
Preview of the email:
Not sure why the above codes are not working.
I know, I can add all variables using
current.variables.<variableName>, but that is time-consuming.
Thanks in advance for your help
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 12:38 PM
If your workflow is running on RITM table (sc_req_item) and you dont need to create a catlog task then, then you dont need to do current.request_item. Instead try current.sys_id.
Start your script with below, no need to use gliderecord:
var keys = new Array();
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(current.sys_id);
set.load();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 12:38 PM
If your workflow is running on RITM table (sc_req_item) and you dont need to create a catlog task then, then you dont need to do current.request_item. Instead try current.sys_id.
Start your script with below, no need to use gliderecord:
var keys = new Array();
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(current.sys_id);
set.load();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 10:55 PM
This is working as needed. I was thinking similar but was confused as well.
Thanks for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 12:38 PM - edited 06-01-2023 12:39 PM
This is a mail script we use that was built long ago to write all variables that contain data in various emails within our system. Still works fine today.
template.print("<br/><br/><b><u>Requested Item Details:</u></b><br/>");
var item = new GlideRecord("sc_req_item");
item.addQuery("request", current.sys_id);
item.query();
while(item.next()) {
template.print("<b>Requested Item Number:</b> " + item.number + "<br/>");
var keys = new Array();
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(item.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("<br/> <b>" + vs.get(i).getLabel() + "</b> = " + vs.get(i).getDisplayValue() + "");
}
}
}
We use it in the "Request Opened On Behalf" notification for example.
Please mark this response as correct and/or helpful if it assisted you with your question.
Steven