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
   
}
3 REPLIES 3

Chaitanya ILCR
Mega Patron
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
        g_form.setSectionDisplay("A", newValue == 'A');
g_form.setSectionDisplay("a", newValue == 'A')
    
}

Hi @sharley ,

Try this 

Is the name of the section is correct ? Or backend name of the value "A" correct?

To know the section name 

Follow this

 

The backend name for a ServiceNow form section is its label converted to lowercase, with the first space becoming an underscore and subsequent spaces/special characters removed (e.g., "Caller Details" becomes caller_details). You find these by using the g_form.getSectionNames() API in a client script for dynamic retrieval or by configuring the Form Layout to see the names. 

 

 

RaghavSh
Mega Patron

@sharley You don't need return in this case. Your script is correct, you probably need to check the backend value of type "A" and UI section name should be correct.

 

Refer: https://www.servicenow.com/community/developer-forum/get-form-section-name/m-p/1971485  explanation for section names


Raghav
MVP 2023
LinkedIn

adityahubli
Giga Guru

Hello @sharley ,

This is the corrected Client Script.

Here, I’ve used city instead of type. If city == 'pune', then only Section A will be shown. You can modify this logic as per your requirement.

In your case:

  • Select field → type

  • Client Script type → onChange

  • Section name → use lowercase backend name

 

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

        g_form.setSectionDisplay('a', false);
// section backend name is  in lowercase , If section Name is 'A' then its backendname is 'a'
        return;
    }
    if (newValue == 'pune') {
        g_form.setSectionDisplay('a', true);
    } else {
        g_form.setSectionDisplay('a', false);
    }
    //Type appropriate comment here, and begin script below

}
 
 

Issues in your original code:

  1. You used onLoad instead of onChange.

  2. You assigned a value using = instead of comparing using ==.

  3. The main logic was written inside the isLoading condition, which should be outside.

If this helps you then mark it as helpful and accept as solution.
Regards,
Aditya,
Technical consultant