- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 12:49 AM
Hello,
I am stuck on a client script or rule, where I have to make one field read only until other is filled. I am confused because onLoad() function should be read only and then I have to have onChange() so when I change the value the field should, so what is the approach? Can anybody help me please and share an example code how to approach this? Thank you!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 12:54 AM
Why don´t you use a UI Policy?
Example: You have to fields "Business Service" and "Assignment Group". As long as the Business Service is empty, the Assignment Group should be readonly.
You can use a UI Policy with condition ~ "Business Service ISEMPTY" -> UI Policy Action: Assignment Group = readonly.
Of course this could also be handled with client scripts. onChange script for field "Business Service" -> if field is empty -> g_from.setReadonly('assignment_group', true);...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 12:54 AM
Why don´t you use a UI Policy?
Example: You have to fields "Business Service" and "Assignment Group". As long as the Business Service is empty, the Assignment Group should be readonly.
You can use a UI Policy with condition ~ "Business Service ISEMPTY" -> UI Policy Action: Assignment Group = readonly.
Of course this could also be handled with client scripts. onChange script for field "Business Service" -> if field is empty -> g_from.setReadonly('assignment_group', true);...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 01:02 AM
Wow, easy, I am not sure why I tried to make it more complicated than it needed to be!!! THANKS A LOT!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2021 11:48 AM
How do you write the condition of "If a field is empty" on client script?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 12:58 AM
Hi,
To make a field read-only with Client Script, refer to the following code example:
function onLoad() {
g_form.setDisabled('number', true);
g_form.setDisabled('date_format', true);
}
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 I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.
Thank you
Cheers
Alberto