Run an encoded query on a current glide record object

Toren
Kilo Guru

Hi All!

It is way too long a story to explain why I need this, but I am looking for a function or something that can run and encoded query against a retrieved gliderecord object to see if it meets the criteria of the the encoded query.

 

Doing this in a widget in service portal.

 

I hope I am explaining that well if not here is code to try to help explain what I need:

 

var gr = new GlideRecord("table_name");

gr.get("342sysid54325");

var boolean = gr.EncodedQueryFunctionThatINeed("active=true^short_description=hellothere^name=yo"); // <---- THE FUNCTION I NEED

if (boolean) {

console.log("The Record is Active, has a short description of hellothere and is named yo");

} else {

console.log("The Record is either not Active, does not have a short description of hellothere  or is  not named yo");

}

1 ACCEPTED SOLUTION

Toren
Kilo Guru

Found out a solution a while back. Just create a new GlideRecord object with the encoded query and the sys_id of the current record you want to check, query the table using the new glide record, and see if it gets an entry back. If it does, it is your record and it matches the query you wanted to check it against. 

View solution in original post

6 REPLIES 6

Jaspal Singh
Mega Patron
Mega Patron

Can you try below.

var gr = new GlideRecord("table_name");
gr.addQuery('sys_id','342sysid54325');
//gr.get("342sysid54325");
gr.query();
if(gr.next())
{
var booleanis=gr.getEncodedQuery();
//var boolean = gr.EncodedQueryFunctionThatINeed("active=true^short_description=hellothere^name=yo"); // <---- THE FUNCTION I NEED
if (boolean.includes('active=true^short_description=hellothere^name=yo')) 
{
console.log("The Record is Active, has a short description of hellothere and is named yo");
} 
else 
{
console.log("The Record is either not Active, does not have a short description of hellothere  or is  not named yo");
}

}

 

Hey Jaspal,

 

That doesn't seem to work. I am not trying to check what the encoded query is. I am more trying to use the encoded query to check the values of certain fields all the same time.

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

This is possible and the syntax is the following:

GlideFilter.checkRecord(GLIDERECORD, ENCODED-QUERY)

This will result in a true or false answer.

So your code example would be:

var gr = new GlideRecord("table_name");
gr.get("342sysid54325");
if (GlideFilter.checkRecord(gr, "active=true^short_description=hellothere^name=yo")) {

console.log("The Record is Active, has a short description of hellothere and is named yo");

} else {

console.log("The Record is either not Active, does not have a short description of hellothere  or is  not named yo");

}

 

Please mark this post or any as helpful or the correct answer to your question if applicable so others viewing may benefit.

Toren
Kilo Guru

Found out a solution a while back. Just create a new GlideRecord object with the encoded query and the sys_id of the current record you want to check, query the table using the new glide record, and see if it gets an entry back. If it does, it is your record and it matches the query you wanted to check it against.