logo

NJP

Hide/Show a Form Field via UI Action Script

Import · Jul 17, 2022 · article

I had a very simple requirement: toggle (hide/show) a form field via an UI Action button. This is achievable - if you first check if the field is visible or not - and then set its visibility (hide it or show it).

I've seen some Community answers on this topic - eg., can't be done, use UI Policy, etc., The one that really works is listed here

To summarize: if you really need to get this done via n UI Action - the code that works is:

(g_form.isVisible(g_form.getGlideUIElement("FIELD_NAME"), g_form.getControl("FIELD_NAME")))

Now, coming to my use-case:

function onCondition(){

    if(g_form.isVisible(g_form.getGlideUIElement("script"), g_form.getControl("script"))){ // Check if the field named "script" is current visible
        g_form.setDisplay('script',false);  // It's visible, so - hide it.
    }
    else{ 
             // The "script" field is currently NOT visible, so show it
        g_form.setDisplay('script',true);
    }

}

image

That's it! It's pretty basic - but I hope it helps someone.

View original source

https://www.servicenow.com/community/developer-articles/hide-show-a-form-field-via-ui-action-script/ta-p/2297343