Determine if current object would be captured in a query

steph3
Tera Contributor

Is there a way to determine if a current object would be captured in a query that has not been inserted? So for example, if a record is not yet inserted and you only have the current object but wanted to determine if it matched up to be captured in a query. Obviously you wouldn't be able to use a gliderecord because it hasn't been inserted. Is there another way?

1 ACCEPTED SOLUTION

If you already have an encoded query and you want to compare it to an object you can use GlideFilter.checkRecord(GlideRecord, EncodedQuery).

So for example:

find_real_file.png

 

If I want to validate whether the incident's caller is Joe Employee and priority is one and incident is active, the encoded query is:

var query = "active=true^priority=1^caller_id=681ccaf9c0a8016400b98a06818d57c7";

Here is a background script I use to test the encoded query against the incident:

var incident = new GlideRecord("incident");
incident.get("695505ca4f629b00d1676bd18110c7b3");
var query = "active=true^priority=1^caller_id=681ccaf9c0a8016400b98a06818d57c7";

var queryMatch = GlideFilter.checkRecord(incident, query);
gs.print("queryMatch: " + queryMatch);

The queryMatch variable will be true if it matches and false if not.  Here is the result:

*** Script: queryMatch: true

Please mark any post as helpful or the correct answer to your question so others viewing can benefit.

 

View solution in original post

5 REPLIES 5

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

See the solution in this very old post:

https://community.servicenow.com/community?id=community_question&sys_id=486b4b21db9cdbc01dcaf3231f96...

Since you have the current object in memory, you can loop through the values to create an encoded query which is what "templateFields" is in that script and then use it to query the table to see if there is a record that matches.  You may need to exclude certain fields, especially dates since they might not match up.

Thanks for your reply Michael! What if I already have the query and I want to know if the current object would match that query?

If you already have an encoded query and you want to compare it to an object you can use GlideFilter.checkRecord(GlideRecord, EncodedQuery).

So for example:

find_real_file.png

 

If I want to validate whether the incident's caller is Joe Employee and priority is one and incident is active, the encoded query is:

var query = "active=true^priority=1^caller_id=681ccaf9c0a8016400b98a06818d57c7";

Here is a background script I use to test the encoded query against the incident:

var incident = new GlideRecord("incident");
incident.get("695505ca4f629b00d1676bd18110c7b3");
var query = "active=true^priority=1^caller_id=681ccaf9c0a8016400b98a06818d57c7";

var queryMatch = GlideFilter.checkRecord(incident, query);
gs.print("queryMatch: " + queryMatch);

The queryMatch variable will be true if it matches and false if not.  Here is the result:

*** Script: queryMatch: true

Please mark any post as helpful or the correct answer to your question so others viewing can benefit.

 

Perfect! That is exactly what I needed! I really appreciate your time.