onchange Catalog Client Script is looping

rai5
Mega Guru

Hello Devs, 

 

 I have been trying to figure out a problem that we started noticing after the Upgrade to Kingston with one of our Catalog client scripts. Might now be related to the upgrade but I think is worth mentioning it. The scripts applies to a variable withing a variable set onchange and it a list collector that has a filter. The issue is that the script is looping and sometimes it loops a lot depending the the result set that is derived from another field (Company). Every that other field (Company) changes even though the on change script does not apply it I can see the script for the module field looping. The bigger the result set from from the company field the more it loops. I was wondering if it could be due to the onchange being triggered on the module field for every record returned. I certainly seems that way. Is there a way to prevent this behavior?  I inserting the code triggered by the onchange catalog script on the module field here. 

 

function onChange(control, oldValue, newValue, isLoading) {
	//Apply a filter to the list collector variable
	if (isLoading) {
		return;
	}
	
	var sL = g_form.getValue('rr_company');
	var sL1 = g_form.getValue('user_company');
	if(g_form.getValue('request_type') == 'New' && sL == '09ca0a06db79aa40e4997bec0f961933' && newValue == 'd4c607c2dbece34084ea72fc0f96199b'){
	return;
	}
	

	
 	var collectorName = 'rr_roles_responsibilities';
 	var filterString = 'u_active=1^u_application='+g_form.getValue('rr_application')+'^u_company='+g_form.getValue('rr_company')+'^u_module='+g_form.getValue('rr_module_sb');
	
	
	//Find the filter elements
	var fil = gel('ep');

	
	//Reset the filter query
	window[collectorName + 'g_filter'].reset();
	window[collectorName + 'g_filter'].setQuery(filterString);
	window[collectorName + 'acRequest'](null);
}

 

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hi Rai,

I don't think we can add multiple option at the same time.

What I could think of overcoming this issue is that you can have 1 extra field String type or any other you could think of better and can do the following:

1) initially set that field value as 'false' in onChange client script that is running when 'Company' field is changed

2) then change the set the value of this new field to true at the end of the Client script as shown below:

for ( var i = 0; i < modules.length; i++) {
		
	g_form.addOption('rr_module_sb', modules[i].getAttribute('id'), modules[i].getAttribute('name'));
}
g_form.setValue('name_of_new_field', 'true');

3) Now you check onChange client script that runs on change of 'Module' field if the value of this new field is true then only run the client script otherwise return as shown below :

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || g_form.getValue('new_field_name') == 'false') {
      return;
}

Please mark this correct/helpful, if it resolves your issue.

Thanks

 

View solution in original post

7 REPLIES 7

Krishnasundar1
Kilo Contributor

Will adding newValue EMPTY check resolve it ?

 

if (isLoading || newValue == "") {
   return;
}

 

Hi Krish I tried that and the loop persists.

rai5
Mega Guru

I think I know exactly why this issues is happing. There is another script in the field that gets triggered onchange for the company variable field. It creates options for the module variable field and adds them one by one in a loop; therefore causing the issue. Is there a way to avoid triggering the field every time the scripts add an option? At the very least there should be a way to create the array and insert it a once it has all the options right? 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	// alert('Running set Module');
	g_form.clearOptions('rr_module_sb');
	g_form.setValue('rr_module_sb', "-- None --", "-- None --");
	g_form.addOption('rr_module_sb', "-- None --", "-- None --");
	//	alert('About to call Ajax');
	var ajax = new GlideAjax('MyModuleAjax');
	ajax.addParam('sysparm_name', 'getModule');
	ajax.addParam('sysparm_rr_application', g_form.getValue('rr_application'));
	ajax.addParam('sysparm_rr_company', g_form.getValue('rr_company'));
	ajax.getXML(setModules);
}

function setModules(response) {
	var modules = response.responseXML.getElementsByTagName("module");
	//	alert(modules[0][0]);
	for ( var i = 0; i < modules.length; i++) {
		
		g_form.addOption('rr_module_sb', modules[i].getAttribute('id'), modules[i].getAttribute('name'));
	}
}

Mahendra RC
Mega Sage

Hi Rai,

I don't think we can add multiple option at the same time.

What I could think of overcoming this issue is that you can have 1 extra field String type or any other you could think of better and can do the following:

1) initially set that field value as 'false' in onChange client script that is running when 'Company' field is changed

2) then change the set the value of this new field to true at the end of the Client script as shown below:

for ( var i = 0; i < modules.length; i++) {
		
	g_form.addOption('rr_module_sb', modules[i].getAttribute('id'), modules[i].getAttribute('name'));
}
g_form.setValue('name_of_new_field', 'true');

3) Now you check onChange client script that runs on change of 'Module' field if the value of this new field is true then only run the client script otherwise return as shown below :

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || g_form.getValue('new_field_name') == 'false') {
      return;
}

Please mark this correct/helpful, if it resolves your issue.

Thanks