Is it possible to show related list conditionally?

Nisar2
Mega Guru

Hi experts,

I've a related list defined on a table. I would like to show this tab only on certain conditions. What would be the best way to achieve this? The screenshot below should define what I'm trying to do

find_real_file.png

 

One way I can think of is making this return 0 rows so that I can then go to "Right Click->Configure Control->Omit if no records"

But I would be all for not having to query that table at all since it contains 3M+ records and querying through such a large dataset is delaying loading of form on the parent table

1 ACCEPTED SOLUTION

Do this

parent object gives you access to current form field values

if(parent.status.toString() == 'active'){
	// use your query to show
}
else{
	current.addQuery("sys_id", "-1"); // no records returned using this
}

Regards
Ankur

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

View solution in original post

22 REPLIES 22

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

are you saying based on some form field value you need to show/hide the related list?

can you explain your complete requirement?

Regards
Ankur

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

are you saying based on some form field value you need to show/hide the related list?

Yes, that is the requirement. Based on a field value on the parent form (i.e. form view on which the related list will be shown), I need to hide or show it.

 

This also includes the requirement of not having to look for related records if I've to hide the related tab.

 

So in short:

(a) When tab needs to be showed, sure go ahead and query the related table

(b) When the tab needs to be hidden, do not even attempt to query the related table since I do not want that information at all (it is possible that there might still be records in the related table when I'm hiding the tab)

 

For example:

Field which decides the show/hide condition: Customer status = "Active"

Related table: Orders placed by that customer

Logic:

if( customer status == active)

{

    // show the orders tab

}

else

{

    // hide the orders tab

   // inactive customers may also have had some orders placed but I do not want to query for them

}

 

Hope this makes sense

Do this

parent object gives you access to current form field values

if(parent.status.toString() == 'active'){
	// use your query to show
}
else{
	current.addQuery("sys_id", "-1"); // no records returned using this
}

Regards
Ankur

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

I'm not quiet sure how much effect using "-1" will have (i.e. whether it will still traverse through all records to find the matching sys_id or will it skip it completely) but this is something that will at least guarantee 0 records in case status condition is not matched.

So I'll tentatively mark it as answer but might come back after performing some SVT.