I

just saw a question on the ServiceNow forums asking how you could show and hide an embedded related list on a form. I just created some client scripts to accomplish this task so I figured I would share them here. I’ll also review the methods and limitations for showing and hiding standard related lists using client scripting.

First of all, if you need to understand the basics about these concepts I’ll refer you to the ServiceNow wiki. Here are a couple of links that should be useful…

Related Lists
Embedded Lists

Hiding and Showing Related Lists

There are some built-in methods for showing and hiding standard related lists. Simply provide the table name of the related list you want to hide (or the ‘Related list’ field value from the list control in some cases). One limitation you’ll want to be aware of is that these methods hide the related list, but they don’t hide the tab if you’re using tabbed forms.

I’m using the ‘Affected CIs’ related list as an example in these scripts. Replace ‘task_ci’ below with the table name of your related list.

Hiding a Related List

g_form.hideRelatedList('task_ci');

Showing a Related List

g_form.showRelatedList('task_ci');

Hiding and Showing Embedded Lists

These scripts were created by me to show and hide form-embedded lists. In this case, it’s easier to target the label of the embedded list so you need to use the list label instead of the table name.

I’m using the ‘Affected CIs’ related list as an example in these scripts. Replace ‘Affected CIs’ below with the label of your embedded list.

Hiding an Embedded List

var list = $$('div[tab_caption="Affected CIs"]')[0];
if(list.hasClassName('embedded')){
   list.hide();
}

Showing an Embedded List

var list = $$('div[tab_caption="Affected CIs"]')[0];
if(list.hasClassName('embedded')){
   list.show();
}