I want to hide all fields of the task based on the one value (state) in the task?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2017 10:46 PM
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.
Please help guys!!
Regards,
Raju Singh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2017 10:50 PM
This is a security requirement and would recommend using ACL's.
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.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2017 10:51 PM
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')

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2017 10:51 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2017 11:08 PM
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