I want to hide all fields of the task based on the one value (state) in the task?

Raju Singh1
Tera Expert

Hi Experts,

As per my requirement, if the state in the task is 'Closed Complete' I want to set all the field values to read only. I have the code which sets all the field values to read only and I have put the code onLoad Client script.

When I put the condition, it doesn't work.

It either sets all the values to read only if I remove this condition if(state1 =='Closed Complete')

function onLoad() {

    //Type appropriate comment here, and begin script below

    var state1 = g_form.getValue('state');

 

  {

var fr = g_form.getEditableFields();

for (var x = 0; x < fr.length; x++)

    if(state1 =='Closed Complete')

g_form.setReadOnly(fr[x], true);

}

  g_form.setVariablesReadOnly(true);

}

Here is the screen shot of the task and the script.

SCTask.JPG

Onload_script.JPG

Please help guys!!

Regards,

Raju Singh

9 REPLIES 9

The SN Nerd
Giga Sage
Giga Sage

This is a security requirement and would recommend using ACL's.


Access control list rules



Make sure all your Catalog Task > Write ACLS have the condition to 'State' IS NOT 'Closed Complete'.


I would recommend using 'Active = False' as an alternative, so you can have many closed states later on.


OOTB, all Closed states set the record to active=false.


find_real_file.png



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Gurpreet07
Mega Sage

This is because 'Closed Complete' is not the backend value for the selected value. Its should be numeric. Configure the choices for state field and find out the backend value and then update the 'if' condition for that backend value


if(state1 == '3')


Gowrisankar Sat
Tera Guru

Hi Raju,



The best and easiest way to solve this requirement is creating an ACL.



You have create a WRITE ACL on sc_task field.


ACL: sc_task.*


Operation: write


Condition: state is closed complete


Script: answer = false


This will only work if there is no other field level write ACL's are in place, otherwise, they won't get picked up by *.*



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022