GlideRecord troubleshooting getEncodedQuery()

gana1
Tera Expert

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.

5 REPLIES 5

Deepak Kumar5
Kilo Sage

What is your output ?


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.


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.


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.