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

Thank you @Juhi Poddar 

Omkar Mone
Mega Sage

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

Thank you @Omkar Mone 

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."

Thank you @Mimi Edet