GlideRecord troubleshooting getEncodedQuery()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2017 11:17 PM
Hi All,
GlideRecord is a powerful query method in Servicenow. Sometimes we do face issues in queries. In some cases we might not be sure if it is doing the same query what we are trying to feed and expect that to be same. Recently I went through such scenario where GlideRecord was appending an extra query which was not added in the query at all.And I was not sure why it is not getting inside the glidequery loop. It just had 1 query like "Column", variable. Although the variable was getting the value properly the query was not entering inside the loop. Then I thought there must be something breaking in the query itself. But how do you directly get to know what actual query it is running in the backend even though it has just 1 query. Just to troubleshoot this, I used "getEncodedQuery()" in the else loop and i got the query that gliderecord was doing. It was adding an extra filter though it was not mentioned. Below is what I did to get the query what actually system was taking. It went to the else loop and gave the query what system was taking.
var gpr= new GlideRecord('table_name');
gpr.addQuery('column_name',variable);
gpr.query();
if(gpr.next()){
gs.log("Group query is: "+gpr.getEncodedQuery());
return true;
}
else{
gs.log("Group query is: "+gpr.getEncodedQuery());
return false;
}
This might look simpler, but I see it is really useful when you get such issues.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2017 11:39 PM
What is your output ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2017 01:28 AM
I was doing something like below:
var gpr = new GlideRecord('sys_user_group');
gpr.addQuery('u_integration_id',variable);
gpr.query();
if(gpr.next()){
//do
}
But the query system was doing is
u_integration_id=variabledata^active=true^nameSTARTSWITHNL ^active=true
As you see it is adding extra filter as nameSTARTSWITHNL. It could be from somewhere [query BR or something else] taking and adding. But the more I was concerned about how to see the exact query system was doing. GetEncodedquery helped me get the same.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2017 01:51 AM
Have you checked for any Query-based Business Rules on that table?
What you could do is enable Session Debug > Debug Business Rule. After running that script, navigate to an empty page (e.g. ui_page.do) in another tab and review the debug information you see there. If you also enabled Debug Log, you will find at which point the script was executed. The statements after may help identify any query BRs that could be running and impacting your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2017 02:02 AM
Yes, Thats fine. That would be the next step.There are multiple ways to find that or stop as well. Like I said I was more concerned about the actual query the system was doing which was the first step to find out the issue.