Variables in Mail Scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2017 01:31 PM
I have read through all of the wiki blogs around this, but I'm still having two issues with the mails scripts.
1) The first issue is that I'd like to hide the unanswered variables and only display the answered variables. How do I correct the if line in order to accomplish this? Here is my script:
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.sysapproval);
item.query();
while(item.next()) {
template.print("Options:\n");
var keys = [];
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() != '') { //This displays all of the variables (answered/unanswered)
// if(vs.get(i).getLabel() != '' && vs.getDisplayValue() != '' && visible == true); { //I've tried this line but it hides all of my variables
template.space(4);
template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");
}
}
}
2) My second mail script issue is that I've created a custom table and I'm having a hard time with displaying the variables within my mail script/notifications. I'm pretty sure it is the addQuery line that is causing the issue, but what should this line be? here is that script:
var item = new GlideRecord("x_tebc2_fac_manage_fac_management_table"); //custom table
item.addQuery("sys_id", current.sys_id); //I have tried different addQuery but nothing seems to work
item.query();
while(item.next()) {
// template.print(item.number + ": " + item.quantity + " X " + item.cat_item.getDisplayValue() + "\n");
template.print("Options:\n");
var keys = [];
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");
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2017 01:38 PM
For #1, try just vs.get(i).getLabel() != '' && vs.get(i).getDisplayValue()!=''
For the second, I'm pretty sure you can skip the glide record query if the notification is being sent from the same table, just grab your variables where setRequestID(current.sys_id.toString());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2017 02:45 PM
Hi Kristen,
Thanks for taking a look at this. I've tried both changes that you've suggested but I'm still having the same problems. Do you have any other suggestions? This is what I've changed in my scripts:
1) Requested Items:
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.sysapproval);
item.query();
while(item.next()) {
template.print("Options:\n");
var keys = [];
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() != '') {
// if(vs.get(i).getLabel() != '' && vs.getDisplayValue() != '' && visible == true); {
if (vs.get(i).getLabel() != '' && vs.get(i).getDisplayValue()!='');
template.space(4);
template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");
}
}
2) Custom Application: Also, should this mail script be created in the global or the custom application? I've tried both with the same results but I've kept the global script.
var item = new GlideRecord("x_tebc2_fac_manage_fac_management_table"); //custom table
// item.addQuery("sys_id", current.sys_id);
// item.query();
// while(item.next()) {
// template.print(item.number + ": " + item.quantity + " X " + item.cat_item.getDisplayValue() + "\n");
template.print("Options:\n");
var keys = [];
var set = new GlideappVariablePoolQuestionSet();
// set.setRequestID(item.sys_id);
// set.setRequestID(current.sys_id.toString()); //Ive tried it this way as well.
set.RequestID(current.sys_id.toString());
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");
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2017 06:10 AM
Hi Julie - Looks like there were two minor issues when I compare this to what I have running against the approval table. First, in setRequestID(), I'm setting the sys_id to string. Second, your if statement had a semicolon instead of the curly brackets.
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.sysapproval);
item.query();
while(item.next()) {
template.print("Options:\n");
var keys = [];
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(item.sys_id.toString());
set.load();
var vs = set.getFlatQuestions();
for (var i=0; i < vs.size(); i++) {
if (vs.get(i).getLabel() != '' && vs.get(i).getDisplayValue()!=''){
template.space(4);
template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");
}
}
}
For the second one, try this (this is assuming your custom table is an extension of the requested item table and is capable of having variables on it, rather than just straight fields):
template.print("Options:\n");
var keys = [];
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(current.sys_id.toString()); //you were missing a "set" here
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");
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2017 02:48 PM
Hi Kristen,
The correction on the first one, for the requested item, worked! Thanks so much for providing that feedback.
Unfortunately the second one didn't work. For further information, the custom table was extended from the task table, not Requested Item table. I created a variable editor (from UI > formatters > UI Macros > com_glideapp_questionset_default_question_editor) in order to view the variables on the form. These variables were created on separate record producers. I was thinking that maybe I need to write something to call to the variable table (item_option_new)? Any other suggestions?
Thanks!