- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2023 07:34 PM - edited 06-25-2023 07:35 PM
Hi,
I have a custom application that I built and I am trying to create a related list to the user table. The two table have a few common fields and I was able to pass the users to the custom table, however, there is an additional condition that I can't seem to get to work.
On the parent (applies to table), I have a field that if it is empty then the query needs to show this set of user that match the conditions, else it will show these users that match the conditions.
I have added the following script to my relationship query with script.
(function refineQuery(current, parent) {
// Add your code here, such as current.addQuery(field, value);
if ((parent.active == true) && (parent.location != '')) {
current.addQuery("active", true);
current.addQuery("title", parent.title);
current.addQuery("cost_center", parent.cost_center);
current.addQuery("location", parent.location);
} else {
if ((parent.active == true) && (!parent.location)){
current.addQuery("active", true);
current.addQuery("title", parent.title);
current.addQuery("cost_center", parent.cost_center);
}
}
})(current, parent);
But it is not working.
How can I get this to work?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 04:32 AM
try this
(function refineQuery(current, parent) {
// Add your code here, such as current.addQuery(field, value);
if ((parent.active == true) && (parent.location != '')) {
current.addQuery("active", true);
current.addQuery("title", parent.title);
current.addQuery("cost_center", parent.cost_center);
current.addQuery("location", parent.location);
} else {
if ((parent.active == true) && (parent.location == '')){
current.addQuery("active", true);
current.addQuery("title", parent.title);
current.addQuery("cost_center", parent.cost_center);
}
else{ // this will run when parent is active false
current.addQuery('sys_id','-1'); // don't show any records
}
}
})(current, parent);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 12:08 AM
HI @Dazler ,
I trust you are doing great.
Based on the script you shared, I can see a couple of potential issues. Let's go through them and make the necessary adjustments:
(function refineQuery(current, parent) {
if (parent.active == true && parent.location != '') {
current.addQuery("active", true);
current.addQuery("title", parent.title);
current.addQuery("cost_center", parent.cost_center);
current.addQuery("location", parent.location);
} else {
if (parent.active == true && !parent.location) {
current.addQuery("active", true);
current.addQuery("title", parent.title);
current.addQuery("cost_center", parent.cost_center);
}
}
})(current, parent);
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi