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

6 REPLIES 6

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.

 

Daniel Gens
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);
}