- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2018 02:32 AM
After looking up how to get the catalogue item variables into an approval email, it said to use an email script called from an amended template and retrieve the records using GlideappVariablePoolQuestionSet
I have two issues with the code below
1. The first section requires a Request level sys_id to work, the current.sys_id in item.addQuery("request", current.sys_id); is approval sys_id and does not return anything. How to I get the request sys_id from the sysapproval_approval record?
2. Whatever level of sys_id I put into this section I get TypeError: Cannot find default value for object, I inserted Request, cat item and sysapproval level Sys_id's to test
var keys = [];
var set = new GlideappVariablePoolQuestionSet();
//set.setRequestID("a00d2838db81af40b0939785ca9619f3");
//set.setRequestID('8384c1c1db0daf40b0939785ca96194b');
//set.setRequestID('8384c1c1db0daf40b0939785ca96194b');
set.setRequestID(item.sys_id);
set.load();
I am new to ServiceNow development so I expect I am doing something ridiculous, but I have found this same code on almost every suggestion on how to achieve this script below. I have seen some sugestions of adding variables to a description field on submit but I am unclear being new to SNOW how to achieve this either any help on this would be really appreciated.
template.print("<br /> Testing Request Notification change<br />");
template.print("<br /> Testing Request: "+current.sysapproval.sc_request);
template.print('<br /> ${mailto:mailto.approval} <br /> <br />');
template.print('<br /> ${mailto:mailto.rejection} <br /> <br />');
template.print("Testing current.sys_id:"+ current.sys_id+": <br />" );
var item = new GlideRecord("sc_req_item");
item.addQuery("request", '3284c1c1db0daf40b0939785ca961903');
//item.addQuery("request", current.sys_id);
item.query();
//template.print("Testing current.sys_id:"+ current.sys_id+": <br />" );
while(item.next())
{
template.print("Item Number:"+item.number + " <br />DisplayValue: " + item.cat_item.getDisplayValue() + ": <br /> ShortDescription: " + item.short_description + " <br /> Item.description" + item.description + " <br /> variabes:" +item.variables);
template.print("<br /> item.sys_id should be ReqItem: "+ item.sys_id + "<br />");
var keys = [];
var set = new GlideappVariablePoolQuestionSet();
//set.setRequestID("a00d2838db81af40b0939785ca9619f3");
//set.setRequestID('8384c1c1db0daf40b0939785ca96194b');
set.setRequestID(item.sys_id.toString());
set.load();
try
{
var vs = set.getFlatQuestions();
template.print("Testing ");
template.print("Testing vs.size : "+ vs.size);}
catch(Err)
{template.print(Err);}
for (var i=0; i < vs.size(); i++) {
if(vs.get(i).getLabel().length > 1) {
template.space(4);
template.print(' ' + vs.get(i).getLabel() + ' = ' + vs.get(i).getDisplayValue() + '<br />');
}
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2018 10:58 PM
For some reason the above still did not work, but I found two other ways around it.
1. current.sysapproval.variables.fieldname allows access to the known named variables which I could not find before.
2. Using a before update business rule to only run on correct catalog item sys ids.
I added all items to the catalog item description field (4000 characters max) which was not an issue as set max_length attribute on all fields on form. I formatted text as fielddescription: + <field value> +’#’. I then separated the strings with string.split in the email script. I also used string replace to add extra space before and after colon to tidy format.
This gave the added advantage of allowing me to encrypt some of the sensitive fields and decrypt them in the email script. I separarated the fields I had encrypted and the ones I had not with ~ and split whole string on that, before decrypting second half of the string.
This solution allowed me to use template on multiple items as the description and field list were built up with line returns. The fact catalog item description is not normally displayed makes this a neat solution. Also allows form fields to be nulled if you have sensitive data as I did in this case, as was emailing HR reference info.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2018 03:19 PM
This should work if you're trying to send details of a single request item associated to an approval record.
template.print("Request item summary:\n");
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.sysapproval);
item.query();
if (item.next()) {
template.print(item.number + ": " + item.quantity + " X " + item.cat_item.getDisplayValue() + " at " + item.price.getDisplayValue() + " each \n");
template.print(" Options:\n");
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.get(i).getLabel() != '') {
template.space(4);
template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");
}
}
}
</mail_script>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2018 10:58 PM
For some reason the above still did not work, but I found two other ways around it.
1. current.sysapproval.variables.fieldname allows access to the known named variables which I could not find before.
2. Using a before update business rule to only run on correct catalog item sys ids.
I added all items to the catalog item description field (4000 characters max) which was not an issue as set max_length attribute on all fields on form. I formatted text as fielddescription: + <field value> +’#’. I then separated the strings with string.split in the email script. I also used string replace to add extra space before and after colon to tidy format.
This gave the added advantage of allowing me to encrypt some of the sensitive fields and decrypt them in the email script. I separarated the fields I had encrypted and the ones I had not with ~ and split whole string on that, before decrypting second half of the string.
This solution allowed me to use template on multiple items as the description and field list were built up with line returns. The fact catalog item description is not normally displayed makes this a neat solution. Also allows form fields to be nulled if you have sensitive data as I did in this case, as was emailing HR reference info.