Hide fields based on condition in a form

vidhya_mouli
Tera Sage

Requirement: When contact has value display contact and hide: u_name, u_phone, u_company and u_email else vise versa

function onLoad() {

    var contact = g_form.getValue('contact'); 
	
    if (contact) {
        // Contact has value → show contact, hide other fields
        g_form.setDisplay('contact', true);

        g_form.setDisplay('u_name', false);
        g_form.setDisplay('u_phone', false);
        g_form.setDisplay('u_company', false);
        g_form.setDisplay('u_email', false);

    } else {
        // No contact → hide contact, show other fields
		g_form.setDisplay('contact', false);

        g_form.setDisplay('u_name', true);
        g_form.setDisplay('u_phone', true);
        g_form.setDisplay('u_company', true);
        g_form.setDisplay('u_email', true);
    }
}

 

When the contact has value it hides the rest of the fields. But when the contact does not have value, it shows other fields and the also the contact field.

 

Why?

1 REPLY 1

Tanushree Maiti
Giga Patron

Hi @vidhya_mouli 

 

As you have done in Client script, Try this:

Select field as Contact

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

   if (newValue != '') {
         g_form.setDisplay('contact', true);
        g_form.setDisplay('u_name', false);
        g_form.setDisplay('iphone', false);
        g_form.setDisplay('company', false);
        g_form.setDisplay('u_email', false);
    } else {
         g_form.setDisplay('contact', false);
        g_form.setDisplay('u_name', true);
        g_form.setDisplay('iphone', true);
        g_form.setDisplay('company', true);
        g_form.setDisplay('u_email', true);
    }
}

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: