
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2017 01:17 PM
Hello,
I am having problems getting my addQuery or addEncodedQuery statements work correctly. I am trying to provide a summary of answered options/variables from the Request Item catalog form. But I want to remove all the 'null', false, undefined, [blank], along with some specific options/variables from being listed out in the Task 'Description' field. I created queries using the ServiceNow list query builder, so hopefully, they are correct. I found one article saying to have addEncodedQuery before addQuery but didn't seem to help much. Can you assist me my code or logic in how I am trying filter out unwanted catalog options/variables?
var fltr1 = "item_option_newNOT LIKEMembers^ORitem_option_new=NULL^item_option_newNOT LIKEINCORRECT^ORitem_option_new=NULL"; // Filter 'item_option_new' of "Members" and "INCORRECT"
var fltr2 = 'value!=false^ORvalue=NULL^value!=undefined^ORvalue=NULL'; // Filter 'value' for "false" and "undefined"
var gr = new GlideRecord('sc_req_item');
gr.addQuery('request', current.request.sys_id);
gr.query();
while(gr.next()) {
// Get Owned Variables for Requested Item and sort by Order
var ownvar = new GlideRecord('sc_item_option_mtom');
ownvar.addEncodedQuery(fltr1); // Breaks command task.description = items; from working
//ownvar.addEncodedQuery(fltr2); // Breaks command task.description = items; from working
ownvar.addQuery('request_item.number', gr.number);
//ownvar.addQuery('sc_item_option.value','!=',''); // Doesn't seem to filter anything
ownvar.addQuery('sc_item_option.value','!=','false'); // Removes all 'false' options/variables (blank) - works
ownvar.addQuery('sc_item_option.value','!=','undefined'); // Must have this line to work, not sure why, but when REMARKED out it breaks? -works
//ownvar.addQuery('sc_item_option.item_option_new.name','!*','Members'); // Doesn't work
//ownvar.addNotNullQuery('sc_item_option.value'); // Doesn't seem to filter anything but seems logical to use.
ownvar.orderBy('sc_item_option.order');
ownvar.query();
var items = "Summary of " + gr.number + ": " + gr.cat_item.getDisplayValue() + "\n\n";
while(ownvar.next()) {
var field = ownvar.sc_item_option.item_option_new;
var fieldValue = ownvar.sc_item_option.item_option_new.name;
// Print variable name
items += field.getDisplayValue() + ": " + gr.variables[fieldValue].getDisplayValue() + "\n";
}
}
task.description = items;
Thank you,
-Wesley
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2018 05:39 PM
Please see below for how to troubleshoot and successfully build encoded queries:
- Look at the Dictionary Entry for your table to get the the field labels and names
- Build the filter in ServiceNow
- Modify the filter until it returns the result set you want
- Copy the query from the filter breadcrumb
- Copy it into your code (example)
var encQry = 'sc_item_option.valueISNOTEMPTY^sc_item_option.value!=false^sc_item_option.value!=undefined^request_item.cat_item=0241d1b2db4e4700821a3e5c7c9619b8';
gr.addEncodedQuery(encQry );
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2018 05:39 PM
Please see below for how to troubleshoot and successfully build encoded queries:
- Look at the Dictionary Entry for your table to get the the field labels and names
- Build the filter in ServiceNow
- Modify the filter until it returns the result set you want
- Copy the query from the filter breadcrumb
- Copy it into your code (example)
var encQry = 'sc_item_option.valueISNOTEMPTY^sc_item_option.value!=false^sc_item_option.value!=undefined^request_item.cat_item=0241d1b2db4e4700821a3e5c7c9619b8';
gr.addEncodedQuery(encQry );
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022