Client Script - Read only field until other field is filled.

Halo10110
Giga Contributor

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!

1 ACCEPTED SOLUTION

Sebastian R_
Kilo Sage

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);...

View solution in original post

6 REPLIES 6

Sebastian R_
Kilo Sage

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);...

Wow, easy, I am not sure why I tried to make it more complicated than it needed to be!!! THANKS A LOT!!!

How do you write the condition of "If a field is empty" on client script?

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

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