hide a section on change request form

sharley
Tera Contributor
I am trying to hide a section i created on change_request form when the change type is not chosen as "A" and make it visible only when change type is chosen as "A".
 
Client script:
function onLoad(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
var type = g_form.getValue(type);
if (type = 'A'){
    g_form.setSectionDisplay("A", true);
}
else {
    g_form.setSectionDisplay("A", false);
}
      return;
   }
   //Type appropriate comment here, and begin script below
   
}
10 REPLIES 10

Aswin S
Tera Contributor

Hi @sharley ,

There are a few issues in your script:

  • onLoad does not use newValue; that parameter applies to onChange scripts

  • g_form.getValue(type) should use the field name as a string

  • You’re using = instead of a comparison operator

  • The section name must be the actual section name, not the choice value

Correct onLoad script:

function onLoad() {
    var type = g_form.getValue('type'); // use the actual field name

    g_form.setSectionDisplay('A', type === 'A');
}

Best practice: also add an onChange Client Script on the Change Type field so the section shows/hides immediately when the value changes:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;
    g_form.setSectionDisplay('A', newValue === 'A');
}

This will ensure the section is visible only when Change Type is set to A.

Aswin S
Tera Contributor

Hi @sharley ,

Your script has a few issues: onLoad doesn’t use newValue, the field name must be passed as a string, and you used = instead of a comparison.

Correct approach:

function onLoad() {
    g_form.setSectionDisplay('A', g_form.getValue('type') === 'A');
}

Best practice: Add an onChange Client Script on the Change Type field to toggle the section dynamically.

This will show the section only when Change Type is A.

adityahubli
Tera Guru

Hello @sharley ,

If my response helps you then mark it as helpful and accept as solution.

Regards,

Aditya,

Technical Consultant

Tejas Adhalrao
Tera Guru

You need to write the onchange and on load client scripts , aslo check if there is any Ui policy created because ui policy has more priority than client scrips:

 

on change cline  script : this will hide the section  when you field  type changes

 

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

    // Replace 'type' with your actual field name if different
    if (newValue === 'A') {
        g_form.setSectionDisplay('your_section_name', true);
    } else {
        g_form.setSectionDisplay('your_section_name', false);
    }
}

and this in on load client script : this execute the when you  open the form

function onLoad() {
    var type = g_form.getValue('type');
    if (type === 'A') {
        g_form.setSectionDisplay('your_section_name', true);
    } else {
        g_form.setSectionDisplay('your_section_name', false);
    }
}

 

i

Ankur Bawiskar
Tera Patron

@sharley 

you can have simply onChange which can run onLoad as well

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

    var type = g_form.getValue('type');

    // Replace 'type' with your actual field name if different
    if (newValue === 'A') {
        g_form.setSectionDisplay('sectionName', true);
    } else {
        g_form.setSectionDisplay('sectionName', false);
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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