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 11:10 PM
If its for fields only on the sc_task then go for an ACL. If you want the variables to be readonly as well then go for client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2017 12:16 AM
Hi Gurpreet,
I tried putting your code, but it makes all the values read only except the State. I want the state value also to be read only if it's Closed Complete ..Here's the code.
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 =='3')
g_form.setReadOnly(fr[x], true);
}
g_form.setVariablesReadOnly(true);
}
Please find the screen shot of the SCTask.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2017 12:30 AM
Hi Raju,
There may be a conflicting client script/UI Policy that setting the state to editable. You may need to review your existing UI Policies/Scripts.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2017 02:34 PM
Hey Raju,
It is not considered good practice to do these via Client Scripts. I'll explain the reasons below:
- Client Scripts slow down form load times
- When the form loads, there will be a brief moment where fields are editable before they become read-only
- This isn't a great experience for users
- It is generally considered bad practice to use Client Scripts to make fields read-only
- UI Policy or ACL should be used as a first option
- A user can disable JavaScript, or simply modify the DOM in the console to bypass these rules
- If any script fails before your onload script, it will not run
- Client scripts do not apply to lists or mass updates, so users can still edit records this way
- Client Scripts run before UI Policy. Any existing UI policy will override your scripts
Write ACL's on the other hand
- Is not overridden by UI Policy or Client Script
- Prevent users from modifying the fields in all areas of the tool
- This includes editing from lists
- The fields will display as read-only when the form loads - no delay
- Doesn't bloat your form with JavaScript (Faster form loads - happier users)
ACL's may seem scary, but please consider using them for this requirement.
Your future self and future developers working on your system will thank you
Regards,
Paul
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-07-2017 09:56 PM
Hey Paul,
Thank you for taking out your precious time to explain this in brief mate!! It was very informative and useful.
I appreciate that!!
Best Regards,
Raju Singh