Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

onChange client script based on Multiple field

Shiraz2
Mega Guru

I am using the following script which works fine as an onLoad client Script. However, I was wondering if it is possible to revise it to make it work as an onChange Client script

function onLoad() {

    //Type appropriate comment here, and begin script below

      g_form.setDisplay('u_test_case_count', false);

  var tcCount = g_form.getIntValue('u_test_case_count');

  var i = tcCount;

  for(i = 1; i <= tcCount; i++){

  var varName = g_form.getValue("u_"+i+"_pass_fail");

  if (varName == 'Fail'){

  g_form.setDisplay('u_add_new_act_result',false) ;

  }

  }

}

Little bit about what the code is.

I have about 15 Choice fields (u_X_pass_fail, where X= 1-15) that display on a form one after another. This is achieved by clicking a button called Add New(u_add_new_act_result) which also adds a counter to another field called TC Count(u_test_case_count) . Once the choice field appears and if the selection of 'Fail' is selected,   I want that Add New button to hide/disappear. I can achieve this using UI policy very easily, but client script would work easily.

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Shiraz,



Short answer: You need an onChange client script for each field. There is no universal "onChange for all" concept when dealing with onChange client scripts. You specify which field to watch and when it changes, the script reacts to that changed field/value.



Docs: Client Scripts


Docs: GlideForm


Client Script Best Practices - ServiceNow Wiki      


View solution in original post

7 REPLIES 7

Hi,

I came across a strange scenario using onChange Client scripts. 
I wrote two onChange Client scripts on two variables and setting the values based on the other variable and vice versa. This is working fine on Portal view. But, when it comes to backend, it keep changing the values of the both fields. It's literally dancing. End user wants it to be worked at backend also.

Any resolution ideas or help would be greatly appreciated.

//Client script 1
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {

        return;
    }
    
    g_form.clearValue('provisioning_transport_types', 'true');
    var siteDetails = new GlideAjax('DisconnectCircuitFormSiteCode');
    siteDetails.addParam('sysparm_name', 'getSiteName');
    siteDetails.addParam('sysparm_user', newValue);
    siteDetails.getXML(getData);
}

function getData(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    var result = JSON.parse(answer);
 
    if (g_form.getValue('site_name') != result.code) {
        g_form.setValue('site_name', result.code);

    }

}
//Client Script 2

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

var ga = new GlideAjax('DisconnectCircuitFormDataName2'); //Name of the Script Include 
 ga.addParam('sysparm_name','getAllDetails'); //name of function in script include 
 ga.addParam('sysparm_sysid',newValue); 
 ga.getXML(fieldCallback);
   
}

function fieldCallback(response) {
 var answer = response.responseXML.documentElement.getAttribute('answer');
    var result = JSON.parse(answer);
	if(g_form.getValue('site_code') != result.code){
		g_form.setValue('site_code', result.code);
		
	}

}


Best Regards,
Mani.

 

This is a long dead post, so I'm writing it more for the sake of people trying to find a solution.

Let's assume we have 3 fields dependant (it is scalable):

  • field_1
  • field_2
  • field_3

We create a onChange Client Script on field_1 taking values of all 3 fields. Let's say it's calling AJAX with some input validation.

We need the same script also onChange of field_2 and field_3. 

If we copy over the whole script, then we need to maintain all the copies for any change to the code, that is not good for scaling / maintaining. 

Solution:
Create an onChange script for other fields with only this code:

g_form.setValue('field_1', g_form.getValue('field_1'));

 

This will just set the same value for field_1 which is already there, but it will trigger it's onChange script.

Then you have only one actual script to maintain.

DanielG50278433
Tera Contributor

Hi,

 

For anyone who comes across this post through search engines, I was able to set up an onLoad Client Script that listens to any field changes by registering a handler using g_form.onUserChangeValue() .

 

function onLoad() {
	var handler = function (fieldname, originalValue, newValue) {
		g_form.showFieldMsg(fieldname, 'Old val: '+ originalValue + '\nNew val: ' + newValue, 'info');
		if (fieldname == 'category') {
			//do something
		}
	};
	g_form.onUserChangeValue(handler);
}