How to make fields read only using Client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2017 10:51 PM
I am a newbie to Snow i wrtten client script on incident but fields are not getting read only:
function onLoad() {
//Type appropriate comment here, and begin script below
alert("testing");
g_form.setReadOnly('number', true);
g_form.setReadOnly('date_format', true);
}
- 49,864 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2017 11:09 PM
To make a field read-only with Client Script, refer to the following code example:
function onLoad() {
//Type appropriate comment here, and begin script below
g_form.setDisabled('number', true);
g_form.setDisabled('date_format', true);
}
Reference: https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_GlideFormSetDisabled_String_Boolean
Please Note!
There are a number of ways to make a field read only in ServiceNow.
Using a Client Script should be the last mechanism you choose once all others have been exhausted, such as
- UI Policy
- Data Policy
- Acess Control
- Dictionary Entry
If you can tell us your business requirement, we may be able to guide you to a better solution.
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
08-10-2017 12:14 AM
Paul,
Thanks for the code its not working for Number & Date Format fields, i tried with the same code for Contact type & its working but not working for Number & Date Format.
Can you please tell me what might be the reason
function onLoad() {
//Type appropriate comment here, and begin script below
alert("testing");
// g_form.setReadOnly('number', true);
// g_form.setReadOnly('date_format', true);
g_form.setDisabled('number', true);
g_form.setDisabled('contact_type', true);
//g_form.setReadOnly('contact_type', true);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 12:40 AM
Hi Shaik,
setReadOnly() and setDisabled(), both should work. Can you please cross check if there is not ACL, UI Policy or any other things causing an issue?
Or Can you just increase the execution order or your client script and check.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 03:44 AM
Shishir,
Its my personal instance and there is no ACL or any UI Policy working on it

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 05:25 PM
Put an alert or jslog after your code just to make sure it is running.
Something else is interfering.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022