The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Unable to hide field by using client script

SaitejaN
Tera Contributor

I am trying to hide the field by using Client Script, but I am not by using the script below. Only setMandatory is working, but setVisible is not working.

 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if(newValue){
g_form.setVisible('os_version',true);
g_form.setMandatory('os_version',true);
}else {
g_form.setVisible('os_version',false);
g_form.setMandatory('os_version',false);
}
}
4 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@SaitejaN Try making the field mandatory false first and then set the visible flag.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if(newValue){
g_form.setVisible('os_version',true);
g_form.setMandatory('os_version',true);
}else {
g_form.setMandatory('os_version',false);
g_form.setVisible('os_version',false);
}
}

Another approach to address this requirement is via a UI Policy. Where you can check the field value on the condition builder and check the reverse if false check box. In the UI Policy actions you can make OS version field visible and mandatory.

 

Hope this helps.

View solution in original post

Juhi Poddar
Kilo Patron

Hello @SaitejaN 

You need to set the mandatory field as false and then the visible field to false.

Here is the updated script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if(newValue){
g_form.setVisible('os_version',true);
g_form.setMandatory('os_version',true);
}else {
g_form.setMandatory('os_version',false);
g_form.setVisible('os_version',false);
}
}

 

Refer this servicenow docs for your reference: KB0718566 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

Omkar Mone
Mega Sage

You cannot hide a mandatory field. To hide it, first make it non-mandatory, and then proceed to hide it.

View solution in original post

Mimi Edet
Tera Guru

The behavior you’re experiencing is due to a conflict between the visibility and mandatory state of the field. In ServiceNow, a field cannot be hidden (setDisplay or setVisible) and mandatory (setMandatory) at the same time. When a field is mandatory, the invisible state is effectively ignored because field has to been fulfill.

 

Regards,

Mimi Edet

LinkedIn: https://www.linkedin.com/in/edet-promise/

=========================================================
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

View solution in original post

9 REPLIES 9

James Schwab
Tera Guru

Hi Saiteja, 

Have you considered using a UI Policy? - UI Policies are the recommended solution when you need to dynamically change the behaviour of form elements like fields on predefined conditions like changed values. 

 

Here is the documentation on how to create a simple UI policy: https://www.servicenow.com/docs/bundle/xanadu-platform-administration/page/administer/form-administr....

 

If there is a specific reason you need to use the Client Script, and you are confident that setMandatory is being set to false it suggests that there is another Client Script or UI Policy which is influencing the visibility of this field. 

You could also make a quality of life improvement to your script to make sure it caters for empty values like so:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || isTemplate) {
        return;
    }
    
    // Check if newValue is not empty or null
    var isVisible = (newValue !== "" && newValue !== null);
    
    g_form.setVisible('os_version', isVisible);
    g_form.setMandatory('os_version', isVisible);
}

 



Sandeep Rajput
Tera Patron
Tera Patron

@SaitejaN Try making the field mandatory false first and then set the visible flag.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if(newValue){
g_form.setVisible('os_version',true);
g_form.setMandatory('os_version',true);
}else {
g_form.setMandatory('os_version',false);
g_form.setVisible('os_version',false);
}
}

Another approach to address this requirement is via a UI Policy. Where you can check the field value on the condition builder and check the reverse if false check box. In the UI Policy actions you can make OS version field visible and mandatory.

 

Hope this helps.

Thank you,  @Sandeep Rajput 

Juhi Poddar
Kilo Patron

Hello @SaitejaN 

You need to set the mandatory field as false and then the visible field to false.

Here is the updated script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if(newValue){
g_form.setVisible('os_version',true);
g_form.setMandatory('os_version',true);
}else {
g_form.setMandatory('os_version',false);
g_form.setVisible('os_version',false);
}
}

 

Refer this servicenow docs for your reference: KB0718566 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar