- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-16-2020 11:08 AM
How .addActiveQuery(); behaves on the tables with no 'Active' field? From what I tested, it returns all exisitng table records (like for 'cmdb_ci' table). Would be nice to have to a clear explanation as could not find it elsewhere.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-16-2020 11:35 AM
Hi,
I mentioned this above. It doesn't work...it'll attempt to apply the line because it's an actual query parameter allowed, but it won't apply to the data because that field doesn't exist.
In other cases, GlideRecord will ignore a line if not applicable. This could be from a field not existing or typo'd, etc. It will just move on.
Example:
var gr = new GlideRecord('cmdb_ci');
gr.addActiveQuery();
//gr.addQuery('active', true);
gr.query();
gs.info("get row count: " + gr.getRowCount());
gs.info("get full query: " + gr.getEncodedQuery());
No error on print:
And then if we try the other way with gr.addQuery:
var gr = new GlideRecord('cmdb_ci');
//gr.addActiveQuery();
gr.addQuery('active', true);
gr.query();
gs.info("get row count: " + gr.getRowCount());
gs.info("get full query: " + gr.getEncodedQuery());
We get logger message, but same record count, etc., obviously:
So in either case, the point is, it will basically ignore that part of the query because it doesn't apply. It doesn't show logger for addActiveQuery because there is no class constructor. It returns a GlideQueryCondition object instead.
Here's more information here about it dropping the invalid portion of the query:
This class has no constructor. A GlideQueryCondition object is returned by the following methods:
- addActiveQuery()
- addInactiveQuery()
- addJoinQuery()
- addNotNullQuery()
- addNullQuery()
- addQuery()
If there is a complicated set of AND and OR queries, a single encoded query containing all conditions simplifies the query creation. To simplify the query creation, create a query in a list view, right-click the query, and select Copy query. It creates a single encoded query string to return your result set. Use that string as a parameter in an addEncodedQuery() call.
Always test queries on a sub-production instance prior to deploying them on a production instance. An incorrectly constructed encoded query, such as including an invalid field name, produces an invalid query. When the invalid query is run, the invalid part of the query condition is dropped, and the results are based on the valid part of the query, which may return all records from the table. Using an insert(), update(), deleteRecord(), or deleteMultiple() method on bad query results can result in data loss.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-16-2020 10:16 PM
Allen has explained it very well.
How this method works - this comes from Platform level just like other queries function like addNullQuery() or addNotNullQuery()
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-16-2020 09:33 PM
Hi
Your addActive Query gives you the records for which the below mentioned Active check box is checked i.e. True.
If we go to list view of your table. consider as incident table.
Type incident.list or incident.LIST in the filter navigator and then add active is true you will see 74 records out of 103
this records are nothing but the records which are having the state either New or In Progress or on Hold only.
once the state of your incident goes to resolved or close the active flag becomes false.
add the filter below, you will get same 74 records as previous active true filter
in the script you can add this filter in two ways. either you can copy the filtered query by right click
and copy query and then add it to addEncodedQuery("paste your query"); or you can use addQuery('active','true');
one more way is to use addActiveQuery all these means same.
try this in background scripts
check the results
Please Mark Correct and Helpful
Thanks and Regards
Gaurav Shirsat