Unable to Hide field with UI Policy

Community Alums
Not applicable

I have created a UI policy to hid a field if the project us sub project but its not working.

Also there's a client script working for the same field but for different conditions. For testing I just deactivated the client script and then my UI policy worked but eventually I cant deactivate the Client script. How can I fix this.

4 REPLIES 4

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi,

Can you share what the client script is doing. Seems to be something in there. Remember that you can't hide a field if it's marked as mandatory.

 

//Göran

Community Alums
Not applicable

Hi I can't share the exact script as it contains sensitive data but can tell you the scenerio.

There's a Field(suppose Field_1) in project table which has 3 choices A,B,C. if A or B is selected (Field_E) has to be mandatory and be visible and for C it has to be hidden. so simply used g_form.setMandatory(true) and g_form.setDisplay(true) and vice-versa for Choice C. 

Now the next requirement is to hide (Field_E) if the project record is sub project (parent != empty).

 

Now the UI policy to hide Field_E is not working. Even though I have set madatory as well visible as false in UI Policy.

 

 

Community Alums
Not applicable

The similar scenario client script: 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var Type = g_form.getValue('u_type');
if(Type == 'choice_1'){
g_form.setMandatory('u_code', true);
g_form.setDisplay('u_code', true);
}
else if(Type == 'choice_2'){
g_form.setMandatory('u_code', true);
g_form.setDisplay('u_code', true);
}
else if(Type == 'choice_3'){
g_form.setMandatory('u_code', false);
g_form.setDisplay('u_code', false);
}
else{
g_form.setMandatory('u_code', false);
g_form.setDisplay('u_code', true);
}
}

KapilGera_0-1677736606095.png

 

Nootan Bhat
Kilo Sage

Hi @Community Alums,

Since fields are only visible in Master project, make sure your client script only runs for the Master project.

in that way, client script execution to make the field mandatory or to display will not be executed in Sub project.

 

Another approach is that you can remove the code form the Client script and create new UI policy which will run for Top project only to mandate/display the field.

This way you can hide the fields which are not required in sub project.

Or else you can use client script itself to make the field hidden in case of sub project. In this case you need to have a glide record of the current record to check if parent is empty or not.

 

Let me know if was helpful.

 

Thanks,