How to hide entire section in workspace

Jessica1307
Tera Expert

"Questionnaire Information" and "Service Provider Information" are two sections. I want to hide them if there are no visible fields under them. Is it possible to hide them dynamically?

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@Jessica1307 

try this once

add your correct section name and field names within those sections

Section name is name of section in lower case and space replaced with underscore

function toggleSection(sectionName, fields) {
    var showSection = false;

    for (var i = 0; i < fields.length; i++) {
        if (g_form.isVisible(fields[i])) {
            showSection = true;
            break;
        }
    }

    g_form.setSectionDisplay(sectionName, showSection);
}

function onLoad() {
    toggleSection('questionnaire_information', ['field1', 'field2', 'field3']);
    toggleSection('service_provider_information', ['field4', 'field5']);
}

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

13 REPLIES 13

oh come on @Jessica1307  I am trying to help you here in my free time...

 

Please attach some helpful screenshot to see what section you are referring to. the better question the better answer. I cannot help you without context or visibility. What do you say?

---
Where the rules are real, you'll find me

@GlideFather ,
Sorry to bother you,  Let explain what I want to do.  There are three fields under  "Closure Information" section as below picture

Jessica1307_2-1780489396022.png

There is existed script make these three fields not display on the form based  some condition but the "Closure Information" section still display on the form even though this three fields are hidden on the form( refer below picture).
Now I want to hide this section when the fields belong to it are not display on the form, Do you have any ideas? Thank you in advance!

Jessica1307_0-1780489049452.png

 

@Jessica1307 

if your script knows all the fields are to be marked as hidden then in same script add the logic to hide the section as well

g_form.setSectionDisplay('closure_information', 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar ,
I need to hide many section and I don't know  how many fields marked. So I can't  add the logic directly to hide the section. Is there a way to get the fields under each section and then determine if there are fields displayed on the screen? If there are, then show that part; if not, hide the section.

@Jessica1307 

I shared sample script below

try using this to check if field is visible or not

g_form.isVisible('field1'); // returns true or 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader