- 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-25-2023 10:16 PM
are you sure if the location is empty the parent record is active
if not then it won't work
I just updated the script now
(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);
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 04:10 AM
Hi @Ankur Bawiskar,
Yes, you are right. There are times that the parent record is inactive, whether there is a location or not. I didn't account for that. How can I make it so that if the parent record is inactive to not bring display any user records?
- 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 04:35 AM
That worked. Thank you!!!