The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to check whether a field is Read Only or not from Client Script.

Lohith
Giga Expert

Hi All,

Could you please help me in how to know whether a particular field is Read only or not from Client Script. Like i want to know whether a field "request_state" is read only or not in sc_request table from script.

Thanks and Regards

Lohith.

1 ACCEPTED SOLUTION

Hello,



We found a solution to this problem by inspecting the form HTML. For every field on the form ServiceNow generates a status span that keeps track of the current field input status. When the field is set to read-only, either by the server or through a client script, this field has the class 'read_only'. Therefore we use the following function to check if a field is readonly:



function( fieldname ) {


        var elem = document.getElementById( 'status.' + g_form.getTableName() + '.' + fieldname );


        return elem.hasClassName( 'read_only' );


}



Hope this helps!



Joris


View solution in original post

6 REPLIES 6

Anurag Tripathi
Mega Patron
Mega Patron

HI,



You might need to do some research as to how the html attributes are set when a field is marked read only.


then using g_form.getControl() function you should be able to read that attribute and decide whether the field is marked readonly or not.


-Anurag

Hello,



We found a solution to this problem by inspecting the form HTML. For every field on the form ServiceNow generates a status span that keeps track of the current field input status. When the field is set to read-only, either by the server or through a client script, this field has the class 'read_only'. Therefore we use the following function to check if a field is readonly:



function( fieldname ) {


        var elem = document.getElementById( 'status.' + g_form.getTableName() + '.' + fieldname );


        return elem.hasClassName( 'read_only' );


}



Hope this helps!



Joris


Community Alums
Not applicable

Hi,


        I know you posted this some time ago, but I was wondering if this is still use able in Helsinki and how I might implement it.



I'm trying to have it lookup the value of a field titled u_asset_tag but so far in all my efforts have been unable to return a value?



Any help is greatly appreciated.


Thank you


pratulagarwal
Mega Expert

Hi Lohith,



I think you can not get to know that within the client script.



As the fields are made read only in client side code and the client script in which you want to determine weather the field is read only or not, will also run on the client.



And more over the fields become read-only only after the form gets rendered, and at that time all the client side execution are already completed.



Regards


Pratul Agarwal