How to optimize the the query on related list?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 11:57 PM - edited 02-05-2024 12:03 AM
Here,
Parent = problem
Current = incident
(function refineQuery(current, parent) {
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2024 01:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2024 02:11 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2024 02:40 AM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 11:27 PM
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);