how to use on change client script to make fields disabled?

Servicenow10
Kilo Guru

hi 

I have one form where I have one category  drop down field with 3 options:

category: america , asia, london

now based on this category i need to disabled fields 

  1. if i select america only my location field should be changeable (drop down field)
  2. if i select asia then zone  field should be changeable (single line text)
  3. if i select london then place field  field should be changeable  (single line text)

and on load of this form i need location,zone, place should only be readable..... how can i achieve this am new to snow so please help me to complete this

 

thanks in advance!!

 

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Use onload client script for setting location, zone & place field to be read-only by below script.

function onLoad() {
   //Type appropriate comment here, and begin script below
	g_form.setReadOnly('location',true);//replace locationwith appropriate dictonary name of field

   g_form.setReadOnly('zone',true);//replace zone with appropriate dictonary name of field
g_form.setReadOnly('place',true); //replace place with appropriate dictonary name of field
}

 

Use onChange client script as below for Category field

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   //Type appropriate comment here, and begin script below
	var catis=g_form.getValue('category');
	if(catis=='America')
   {
	   g_form.setReadOnly('location',false);
   }
	else if(catis=='Asia')
   {
	   g_form.setReadOnly('zone',false);
   }
	
		else if(catis=='London')
   {
	   g_form.setReadOnly('location',false);
   }
}

 

Make sure you replace the dictonary name accordingly as per the names in the instance.

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.

View solution in original post

3 REPLIES 3

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Use onload client script for setting location, zone & place field to be read-only by below script.

function onLoad() {
   //Type appropriate comment here, and begin script below
	g_form.setReadOnly('location',true);//replace locationwith appropriate dictonary name of field

   g_form.setReadOnly('zone',true);//replace zone with appropriate dictonary name of field
g_form.setReadOnly('place',true); //replace place with appropriate dictonary name of field
}

 

Use onChange client script as below for Category field

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   //Type appropriate comment here, and begin script below
	var catis=g_form.getValue('category');
	if(catis=='America')
   {
	   g_form.setReadOnly('location',false);
   }
	else if(catis=='Asia')
   {
	   g_form.setReadOnly('zone',false);
   }
	
		else if(catis=='London')
   {
	   g_form.setReadOnly('location',false);
   }
}

 

Make sure you replace the dictonary name accordingly as per the names in the instance.

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.

thankyou so  much for your time it worked!!

Lukasz Bojara
Kilo Sage

Hi,

The easiest way to do this is to use UI Policy. Also when it comes to making fields visible/hidden, read-only or mandatory it is Best practice to use UI Policy. And if you do not need to hide entire sections in most case this a no-code approach.


For example, make a field visible if category is America:

 

find_real_file.png

 

Result will be this:

find_real_file.png

 

find_real_file.png

 

This way you can group behaviours based on the conditions. E.g. when a category is America make some fields visible, some hidden, some readonly etc.

 

Hope this helped.

 

Best regards,
Łukasz