How .addActiveQuery(); behaves on the tables with no 'Active' field?

P-Rudenko-SN
ServiceNow Employee
ServiceNow Employee

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.

1 ACCEPTED SOLUTION

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:

find_real_file.png

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:

find_real_file.png

 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.

From: https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideQueryCond...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

6 REPLIES 6

Allen Andreas
Administrator
Administrator

Hi,

So "addActiveQuery" basically just uses addQuery('active', true); in a more "shorthand way".

This is evident by testing within a background script and then printing out the encodedquery of what has been done thus far:

var gr = new GlideRecord('incident');
gr.addActiveQuery();
gr.query();
gs.info("get row count: " + gr.getRowCount());
gs.info("get full query: " + gr.getEncodedQuery());

Which would print this for example:

find_real_file.png

If you don't have an "active" field on your table, then this line is simply ignored within your GlideRecord query as non-applicable.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thanks Allen, this is what I was aware of. However, I was asking how does the method works, when the query runs against the table, that doesn't have the 'Active' field?

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:

find_real_file.png

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:

find_real_file.png

 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.

From: https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideQueryCond...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

@Allen A many thanks Allen for the details, it's all clear now.