onChange client script running multiple times due to another onLoad script

Ahmad6
Giga Expert

Hi,

I have two choice fields that are populated in client scripts, with one field dependent on the other. The "level" choice options are populated based on the location field, and the "area" choice options are populated based on the level field. I have two client scripts that run on the area field, one onLoad and one onChange of the level field, and have the condition "if(isLoading) return" on the onChange client script to ensure there is no duplication of scripts.

My problem I am having is that when the onLoad script for the level field runs, each g_form.addOption() function called triggers the onChange client script since the form is no longer "loading" according to the system. Is there a way to prevent the onChange from triggering multiple times or is there a different way I can build my client scripts?

 

//customise area field onLoad
function onLoad() {
	var area = g_form.getValue('u_area');
	g_form.clearOptions('u_area');

	g_form.getReference('u_client', function(location){
		if(location.name == 'South Site'){ 
			g_form.clearOptions('u_area');
			switch(g_form.getValue('u_level')) {
				case 'Level G':
					g_form.addOption('u_area', 'Ground', 'Ground');
					g_form.addOption('u_area', 'Grey Carpark', 'Grey Carpark');
					g_form.addOption('u_area', 'Purple Carpark', 'Purple Carpark');
					break;
				//more cases....
			}
		}
	});
	g_form.setValue('u_area', area, area);
}

 

//customise level field onLoad
function onLoad() {
	var level = g_form.getValue('u_level');
	var area = g_form.getValue('u_area');
	g_form.clearOptions('u_level');

	g_form.getReference('u_client', function(location){
		if(location.name == 'South Site'){
			g_form.addOption('u_level', 'Docks', 'Docks');
			g_form.addOption('u_level', 'External', 'External');
			g_form.addOption('u_level', 'Level G', 'Level G');
			//more addOption...
		}
	}
	g_form.setValue('u_level', level, level);
	g_form.setValue('u_area', area, area);
}

 

//Customise area field onChange of level field
function onChange(control, oldValue, newValue, isLoading, isTemplate) {

	if (isLoading) {
		return;
	}
	g_form.clearOptions('u_area');
	g_form.getReference('u_client', function(location){
		if(location.name == 'South Site'){ 
			g_form.clearOptions('u_area');
			switch(g_form.getValue('u_level')) {
				case 'Level G':
					g_form.addOption('u_area', 'Ground', 'Ground');
					g_form.addOption('u_area', 'Grey Carpark', 'Grey Carpark');
					g_form.addOption('u_area', 'Purple Carpark', 'Purple Carpark');
					break;
				//more cases...
			}
		}
	});
}

My onChange client script uses g_form.clearOptions('u_area') to ensure that the options are updated to match the selected level, but when someone opens an existing record the value is wiped and the first option is selected .In the above example, any time a record for South Site is opened and the Level selected was Level G, the Area always wipes and is set to Ground, and if the user is unaware and saves the form then the data has been corrupted.

2 REPLIES 2

Chandu Telu
Tera Guru
Tera Guru

HI ,

 

 

Please check is "oldValue" is empty or not .

 

if it's empty means is new record else it's existing record

 

Please mark this answer is correct if the above solution works

Hi Chandu,

 

I am opening an existing record, oldValue is not empty in the client script.

 

the u_level field has no choices by default. on page load one client script populates the choice field by running multiple 'g_form.addOption()' functions. This is causing the onChange client script on the u_level field to run for every addOption function call, so the value saved in u_area is being lost after the second onChange script.