The CreatorCon Call for Content is officially open! Get started here.

Make all form fields read-only (client script)

brumiou
Mega Guru

Hi,

I need to put all the fields of a form read-only. I need to use a client script for that, because in certain case I will need to have some fields editable.

Is there another way than put all the fields in a ui policy (or a client script)?

Is suppose there is a way to do such a thing :

get references to all fields of the form

while(...)
{
referenced field.setreadOnly()
}

Thanks a lot

rgds

Xavier

16 REPLIES 16

Amit Jain2
ServiceNow Employee
ServiceNow Employee

You need to select 'All' in the Script Tab for the field 'Run scripts in UI type'. Then it will work for Mobiel as well.

Thank you for this! I was having some difficulties creating an access control that does the same thing. But this works like a charm. 

This is working fine for platform, but not for agent workspace.

Do you have any alternate suggestion for that?

 

Swarup Patra
Kilo Guru

Sure, you can make all form fields read-only using a client script in ServiceNow. Here's a sample code: javascript function onLoad() { //Loop through each field in the form var control = g_form.getControl('variable_name'); for (var i = 0; i < control.length; i++) { //Make each field read-only g_form.setReadOnly(control[i].name, true); } } Here are the steps to implement this: 1. Navigate to System Definition > Client Scripts in ServiceNow. 2. Click on New to create a new client script. 3. Fill in the necessary fields: - Name: Give a name to your client script. - Type: Select "onLoad". - Table: Select the table where you want to apply this script. 4. In the Script field, paste the above code. 5. Click on Submit to save the client script. Please replace 'variable_name' with the actual field name you want to make read-only. This script will make all fields read-only when the form loads. nowKB.com

Hi 

You can simply use below code

var getFields = g_form.getEditableFields();

for(var i=0;i<getFields.length;i++)

{

g_form.setReadOnly(getFields(i),true);

}