Blog

Related List Queries (RLQUERY)

One features of ServiceNow you may have missed is the ability to utilize Related List Queries (RLQUERY) using GlideRecord.

What is RLQUERY?

Related List Query is something you can add to an Encoded Query to query a child table within one GlideRecord query.

RLQUERY is what Related List Conditions in reports use. If you build a report with Related List Conditions, and then view the report’s xml, you can see the filter field it is using RLQUERY.

The value of using RLQUERY over other methods like GlideAggregate is uncertain for me. Seems fast and easy to use once you understand it. It is an interesting new way to make queries.

RLQUERY BUILD TRICK

Here is a good way to build scripts using RLQUERY without having to understand RLQUERY that well.

  1. Build a report using Related List Conditions

  2. Reports > Administration > All

  3. Open the Report

  4. Hamburger > Show XML

  5. View the “filter” field in the xml

  6. Use that RLQUERY in your script.

Below are some examples using RLQUERY.

Example 1: Active Incidents with Affected CIs

(function() {
var incidents = [];
var grIncident = new GlideRecord("incident");
grIncident.addActiveQuery();
grIncident.addEncodedQuery('RLQUERYtask_ci.task,>=1,m2m^ENDRLQUERY');
grIncident.query();
while (grIncident.next()) {
incidents.push(grIncident.getValue("number"));
}
gs.print('Active Incidents with Affected CIs: '+incidents);
})();

Example 2: Incidents WITHOUT SLAS

(function() {
var incidents = [];
var grIncident = new GlideRecord("incident");
grIncident.addEncodedQuery('RLQUERYtask_sla.task,=0^ENDRLQUERY');
grIncident.query();
while (grIncident.next()) {
incidents.push(grIncident.getValue("number"));
}
gs.print('Incidents without SLAs: '+incidents);
})();

Example 3: Groups without Group Members

(function() {
var groups = [];
var grGroup = new GlideRecord("sys_user_group");
grGroup.addActiveQuery();
grGroup.addEncodedQuery('^RLQUERYsys_user_grmember.group,=0^ENDRLQUERY');
grGroup.query();
while (grGroup.next()) {
groups.push(grGroup.getValue("name"));
}
gs.print('Groups without Group Members: '+groups);
})();