Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Related List Custom Table

Dazler
Mega Sage

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?

1 ACCEPTED SOLUTION

@Dazler 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Amit Gujarathi
Giga Sage
Giga Sage

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