We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to optimize the the query on related list?

priyankagavali
Tera Contributor
How we can  optimize the below the query on related list?
Here,
Parent = problem
Current = incident


(
function refineQuery(current, parent) {
 
current.addEncodedQuery('problem_id='+parent.getUniqueValue()+'^NQparent_incident.problem_id='+parent.getUniqueValue());
 
})(current, parent);
4 REPLIES 4

Weird
Mega Sage

So you're querying for incidents related to your current problem?
Your query is "Problem = current problem" OR "Incidents parent's problem is current problem".

There are two things i see here. First you've used ^NQ for some reason. Not sure if that slows anything down, but you could just use ^OR instead.
Also, your ^NQ part says parent_incident.problem_id instead of parent.parent_incident.problem_id, so you're not referencing the parent's problem correctly and might end up with incorrect results.

Is this actually working correctly and you're asking to optimize it or is this just a fix request? It's good to note that fixing != optimizing.

Hi @Weird ,

The query is working correctly with ^NQ as we need reference from problem fields and that's possible only with grouped OR .So the query is correct asking how to optimize it.

But as mentioned your script says "parent_incident.problem_id" instead of "parent.parent_incident.problem_id", so you're potentially getting records that you don't want or not getting enough records as the query is broken. 


Also NQ is a bit more suspectible to breaking. For example you can't use it on query rules, so I don't see why you'd need to keep it since the only point for NQ is when you need a very specific query. Potentially Servicenow tries to check for something that doesn't exist which could slow down the query a little.


Here's also a bit of clarification on why NQ seems unnecessary for you.

 

active = true
category = Hardware
OR
active = false
category = Software

 

Here you can see how you're only getting active hardware and inactive software. Very useful since you won't get a mix of inactive and active hardware and software.

In your case your query is

 

This is true: problem_id = parent.sys_id
OR
This is true: parent_incident.problem_id = parent.sys_id

Which doesn't add anything over
This is true: problem_id = parent.sys_id or parent_incident.problem_id = parent.sys_id;

 

 

Amit Pandey
Mega Sage

Hi Priyanka,

 

Can you try this-


(function refineQuery(current, parent) {
    var problemID = parent.getUniqueValue();
    current.addEncodedQuery('problem_id=' + problemID + '^ORparent_incident.problem_id=' + problemID);
})(current, parent);