- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 11:45 AM
Here is my code. I am hoping someone can help me with where I am going wrong. This is an email script where I am cycling through variables that have been marked to include = true and printing only those variables to the notification instead of all the non-empty ones.
(function runMailScript(current, template, email, email_action, event) {
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.sysapproval);
item.query();
while (item.next()) {
var var_labels = [];
vars = new GlideRecord("item_option_new");
vars.addQuery("cat_item", current.sysapproval.cat_item);
vars.addQuery("u_include_in_approval_info", true);
vars.query();
while (vars.next()){
var_labels.push(vars.question_text.toString());
//gs.info("JAL var_label = "+vars.question_text);
}
gs.info("JAL var_label = "+var_labels);
var keys = new Array();
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(item.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>Approval Details</u></b></p>");
for (var i = 0; i < vs.size(); i++) {
gs.info("JAL found: "+vs.get(i).getLabel().toString());
//gs.info("JAL var true " +var_labels.indexOf(vs.get(i).getLabel().toString()));
if ((var_labels.indexOf(vs.get(i).getLabel().toString()) != -1) && 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);
Log statement of the values of the array looks like:
JAL var_label = Indicate Link or URL to unblock,Does exception apply to Element Data,Request type
Log statement of found variables-
JAL found: Indicate Link or URL to unblock
JAL found: Does exception apply to Element Data
JAL found: Request type
However, the log statements where I am checking for the indexOf:
JAL var true -1
Am I formatting the indexOf incorrectly or ? As it stands, nothing is getting printed out for the "include these vars" because it is evaluating to -1 even though the array does include three of the variables.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 12:08 PM
Hi @jlaps
Try this
gs.info("JAL var true " +var_labels.toString().indexOf(vs.get(i).getLabel().toString()));
Syntax :
String1.indexOf(string2) - both string 1 and string 2 should be strings.
https://www.w3schools.com/jsref/jsref_indexof.asp
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 12:08 PM
Hi @jlaps
Try this
gs.info("JAL var true " +var_labels.toString().indexOf(vs.get(i).getLabel().toString()));
Syntax :
String1.indexOf(string2) - both string 1 and string 2 should be strings.
https://www.w3schools.com/jsref/jsref_indexof.asp
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 12:53 PM
This worked, thank you. I had thought since I converted the values in the array to string already, this would not be required... but I added your fix to where it matters and it is working straight away. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 12:10 PM
It looks like you are feeding indexOf specific location in the array but not telling it what to looks for. For example if I have an array (a, b, c, d, e) and I want to do something specific when I see c and I was giving it the location of the array values each time would the loop I would not use indexOf. Rather just something like.
if (vs.get(i).getLable().toString() == "c"){
//Do something
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 12:17 PM
It looks like you are trying to do a mail script to get variables from a request item. This is the code I have been using for years. The only thing it does not display is variables from a multi-row variable set. It is also configured to be able to be used on notifications like approvals.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var item_sys_id;
var tableName = current.getTableName();
if (tableName == 'sysapproval_approver') {
item_sys_id = current.sysapproval;
} else if (tableName == 'sc_task') {
item_sys_id = current.request_item.sys_id;
} else if (tableName == 'sc_req_item') {
item_sys_id = current.sys_id;
} else {
item_sys_id = '';
}
//gs.log("SYS ID : "+item_sys_id);
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", item_sys_id);
item.query();
while (item.next()) {
template.print("<div style='font-size:12pt;font-family:Arial, Helvetica, sans-serif;'><span><b>Options</b>:</span><br />");
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)
var val = vs.get(i).getDisplayValue();
if (val != '') { //Only show variables that have answers.
template.space(4);
template.print(vs.get(i).getLabel() + " : " + val + "<br />");
}
}
}
template.print("</div>");
}
})(current, template, email, email_action, event);