Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Not able to set reference field to read-only using setReadOnly()

mdrafi
Mega Expert

Hi,

I am using client onLoad script to set all editable fields to read-only. But it is not working for reference fields.

function onLoad() {

      //Type appropriate comment here, and begin script below

     

      if (u_status = 'Released')

              {

              var fields = g_form.getEditableFields();

              for (var x = 0; x < fields.length; x++) {

                      g_form.setReadOnly(fields[x], true);

              }

            // g_form.setReadOnly('u_seat_number', true);

              // g_form.setReadOnly('u_name', true);

      }

}

1 ACCEPTED SOLUTION

I tried g_form.setDisabled(), but it didn't work.



But, I have used UI policy and it worded for reference fields.


View solution in original post

4 REPLIES 4

Abhinab Achary1
Tera Guru

i think instaed of readonly you can user set disable method to lock that field


I tried g_form.setDisabled(), but it didn't work.



But, I have used UI policy and it worded for reference fields.


Dom Davidson
Giga Contributor

Hi Mahammad,



I believe you don't have "==" in your if condition.   Also please make sure you are comparing apples to apples(value for value) in your if condition.



Please see below of a similar script I wrote for another table, and it works are intended.


Screen Shot 2017-01-23 at 3.29.44 PM.png


Cheng2
ServiceNow Employee
ServiceNow Employee

Hi there,

Sometimes in order to make function calls like `setReadOnly` or `setDisabled` work correctly, it's needed to check are these fields to be set also have extra settings like mandatory, if yes, make sure to un-mandatory them first:

 

g_form.setMandatory(field, false);
g_form.setDisabled(field, true);

 

 

Regards.