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

mohdaqib
Tera Contributor

Hello, try this code as I have written below

var gpr= new GlideRecord('table_name');
gpr.addQuery('column_name',variable);
gpr.query();
gs.log("Group query is: "+gpr.getEncodedQuery());
if(gpr.next()){
gs.log("Group Available");
}
else{
gs.log("Group Not Available");
}

 Thank You,
Mohd Aqib 
Linkedin - https://www.linkedin.com/in/mohd-aqib-5252361b5