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?

13 REPLIES 13

vidhya_mouli
Tera Sage

The point is, it does not work onLoad. This is a read only form and therefor I don't need OnChange client script.

Venky Kshatriy2
Mega Sage

Hi @vidhya_mouli ,
Could you please clarify whether the CONTACT field is auto-populated, or does the user need to enter the information manually?

Venky Kshatriy2
Mega Sage

Hi @vidhya_mouli 

I implemented the UI policy so that when the User field has a value, the First name field is hidden. If the User field does not have a value, the First name field becomes visible and the User field is hidden. You can refer to the images below for further clarification.

VenkyKshatriy2_4-1779865064330.pngVenkyKshatriy2_5-1779865072650.pngVenkyKshatriy2_6-1779865079837.pngVenkyKshatriy2_7-1779865090866.png

If this response addressed your question, please mark it as Helpful and accept it as the solution.

This works for me too. The issue is when the user is empty. It is displaying both User and First name for me.