Client Script

ankitaayyad
Kilo Contributor

Hi Community, 

I am trying to hide the issues related list, when status is compliant and when status is non compliant then the issues related liste should be visible.

Below is the script, please review and let me know if there is anything wrong.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    if (newValue == 'non_compliant') {
        g_form.showRelatedList('Issues');
    } else {
        g_form.hideRelatedList('Issues');
    }
}
6 REPLIES 6

Rafael Batistot
Mega Patron

Hi @ankitaayyad 

 

Your script is almost correct, but there are two critical issues that will cause it to fail or behave unexpectedly in ServiceNow

 

1. Remove ⁠if (isLoading) return;⁠ so the script runs when the form loads, hiding the list by default if the record is compliant.


2.
Use the correct list table or relationship name (e.g., ⁠sn_grc_issue⁠) instead of the UI label ⁠'Issues'⁠.

 

This version might help you 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue === '') return;

var relatedListName = 'sn_grc_issue'; // Update to your exact related list table name

if (newValue == 'non_compliant') {
g_form.showRelatedList(relatedListName);
} else {
g_form.hideRelatedList(relatedListName);
}
}

Tejas Adhalrao
Mega Sage

hi @ankitaayyad  ,

 

instead of client script you can use Ui policy 

1) click on form hearder -> config -> UI policies -> New

2) select trigger condtion 

TejasAdhalrao_0-1792720191393.png

 

3) save your record , once you save record you can see "Ui policy related list action"  

TejasAdhalrao_1-1792720289685.png

 

 new -> select your related list you can hide and show related list 

TejasAdhalrao_2-1792720444034.png

 

 

If this helps, please mark it helpful or accept it as the solution.

Regards,

Tejas

Tejas Adhalrao
Mega Sage

hi @ankitaayyad  ,

Hi,

The isLoading check prevents the onChange script from running when the form is initially loading. If you want the Issues related list to be hidden or shown correctly when the form first opens, you should handle the initial state separately, for example, with an onLoad Client Script using the same condition.

Keep the isLoading check in the onChange script to avoid unnecessary execution during form loading.

 

If this helps, please mark it helpful or accept it as the solution.

Regards, Tejas

Aditya_hublikar
Giga Sage

Hello @ankitaayyad ,

 

Have you tried UI policy to hide related list ?

Check your  hiderelatedList syntax like actual value that you are providing in g_form.hideRelatedList('relationship') ;

Aditya_hublikar_0-1792730516397.png

 

Add proper relationship name as value in hideRelatedList function . refer above incident task related list hide code . I tried it on onload to hideRelatedList , you can try as per your logic just check relationship name correctly .

 

code :

function onLoad() {
   //Type appropriate comment here, and begin script below
  var data= g_form.getValue('priority');
   if(data==1)
   {
    g_form.hideRelatedList('incident_task.incident');
   }
}
 
If this helps you then mark it as helpful and accept as solution.
Regards,
Aditya