Help with client script to hide section on form

dalewechsler
Mega Expert

I've read many posts about this and followed SN Guru posting about this as well. I've tried several versions of the script below and it still won't work:

This is an onChange client script - supposed to hide a section unless Category = XVALUE. Here's my script (info messages are just so I can see what the script is doing). What have I done wrong? The section will not be hidden - shows no matter what. I've also tried as onLoad script - still no dice. Any help greatly appreciated! The info messages, btw, behave as expected. The section will not hide though.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === 'XVALUE') {

          return;

    }

    //Type appropriate comment here, and begin script below

              var sections = g_form.getSections();

      var val = g_form.getValue('category');

              g_form.addInfoMessage(val);

    if (val == 'Firewall'){

  g_form.addInfoMessage('Show ' + val);

  g_form.setSectionDisplay('firewall_info', true);

    }

    else{

  g_form.addInfoMessage('Do not show ' + val);

  g_form.setSectionDisplay('firewall_info', false);

      }

   

}

1 ACCEPTED SOLUTION

Hi Dale,



Simple suggestion is to create a UI policy and add the condition if the change type is so and so and in the UI policy add the variables and make it mandatory true and check reverse if false is checked. So obviously these fields become mandatory false and this client script will also execute.



Or in the UI policy script itself you can write a script execute if true to make the fields mandatory and section visible and in the execute if false, make them mandatory false and make the section visibility as false


View solution in original post

15 REPLIES 15

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Dale,



This line of code should be sufficient for your code. Just check in browser console any error stopping it from being hidden.


g_form.setSectionDisplay('schedule', false);


https://www.servicenowguru.com/scripting/client-scripts-scripting/showhide-form-section/



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Thanks for the reply. I had the simpler script, but still didn't work. I'm using the same script going back and forth between onLoad and onChange to find one that works.


vinothkumar
Tera Guru

Hi Dale,



Check whether is there any field are mandatory in the section, if so make it as mandatory false and then try hiding the section.


Thanks Vinoth - that seemed to be the problem with hiding it, which is unfortunate, as that is one reason for hiding the section - because it has a bunch of fields only relative to one type of change, and they should be mandatory. Was hoping that hiding the fields would also suppress the mandatory attribute. Any way around that?



Thanks again!