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

Thanks Mahendra!

that definitively was the way to solve this issue. Many thanks to all. 

Janel
Kilo Sage

I've noticed this same problem since we went to Kingston as well.  I've seen it both on table client scripts and catalog client scripts.

In my case, it looped itself when the onChange script is setting its own variable/field to a new value. Like, if I were formatting the value of a variable/field onChange and resetting the variable/field value.

Previously if your onChange variable/field was being set in your onChange script, it would set it just fine and it would not run again.  Since Kingston, the onChange script fires every time the variable/field is set, even when it is the same script setting it.

Weird thing is, this doesn't happen in all scripts.  Just random ones it seems like.

adriantan08
Giga Expert

To prevent an infinite loop whenever you have an onchange to change a field then setting the same field to the oldValue, add this in the first IF condition:

 

if (isLoading || newValue === '' || newValue === oldValue