How to make fields read only using Client script

shaik_irfan
Tera Guru

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

}

13 REPLIES 13

The SN Nerd
Giga Sage
Giga Sage

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

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


}


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.


Shishir,



Its my personal instance and there is no ACL or any UI Policy working on it


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