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

Ankur Bawiskar
Tera Patron
Tera Patron

@Dazler 

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);

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

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?

 

 

 

@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

That worked.  Thank you!!!