OnChange script not working as intended

Staxed
Giga Guru

I'm having a difficult time getting this script to do what I want it to do.  It should be looking at the Division (newValue) and then depending on that, clear all options in location and populate with a specific set for each Division.

If I use the script as written below, whenever the form loads, it changes the location field to whatever the first option in the list is.

If I change the first if in the function to (isLoading || newValue === ' '), it then works as intended and doesn't change the location field itself, but it also shows every location for every division instead of just the appropriate locations.  I'm not sure what I'm missing here.

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

	if(newValue=='Charleston')
	{
		g_form.clearOptions('u_hr_location', 'Location');
		g_form.addOption('u_hr_location','Charleston','Charleston');
		g_form.addOption('u_hr_location','Charleston MCP','Charleston MCP');
	}
	
	else if(newValue=='Florence')
	{
		g_form.clearOptions('u_hr_location', 'Location');
		g_form.addOption('u_hr_location','Florence','Florence');
		g_form.addOption('u_hr_location','Florence MCP','Florence MCP');
		g_form.addOption('u_hr_location','Marion','Marion');
		g_form.addOption('u_hr_location','Marion MCP','Marion MCP');
	}
	
	else if(newValue=='Lancaster')
	{
		g_form.clearOptions('u_hr_location', 'Location');
		g_form.addOption('u_hr_location','Chester','Chester');
		g_form.addOption('u_hr_location','Lancaster','Lancaster');
		g_form.addOption('u_hr_location','Lancaster MCP','Lancaster MCP');
	}
	
	else if(newValue=='Midlands')
	{
		g_form.clearOptions('u_hr_location', 'Location');
		g_form.addOption('u_hr_location','Columbia','Columbia');
		g_form.addOption('u_hr_location','Kershaw','Kershaw');
		g_form.addOption('u_hr_location','Midlands MCP','Midlands MCP');
	}
   
}
1 ACCEPTED SOLUTION

ProcessMatters
Giga Guru

You will need an onLoad script to perform essentially the same functionality, as onChange will only fire when the value changes.

View solution in original post

20 REPLIES 20

Hitoshi Ozawa
Giga Sage
Giga Sage

>but it also shows every location for every division instead of just the appropriate locations.  

Sorry but can't replicated the problem in the question. Only selected locations are showing in the drop down instead of all locations.

Is the requirement is as follows?

  1. Show the "Charleston" (the first item in the division drop down) when the form is opened but not show anything in location drop down
  2. Show corresponding location when Division is changed

Sample execution result:

1. when the form is opened. (there is nothing in location drop down)

find_real_file.png

2. When division is selected (fill location drop down with corresponding locations)

find_real_file.png

Variable definition

find_real_file.png

Division Select Box definition

find_real_file.png

Client script:

 

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

    if (newValue == 'Charleston') {
        g_form.clearOptions('u_hr_location', 'Location');
        g_form.addOption('u_hr_location', 'Charleston', 'Charleston');
        g_form.addOption('u_hr_location', 'Charleston MCP', 'Charleston MCP');
    } else if (newValue == 'Florence') {
        g_form.clearOptions('u_hr_location', 'Location');
        g_form.addOption('u_hr_location', 'Florence', 'Florence');
        g_form.addOption('u_hr_location', 'Florence MCP', 'Florence MCP');
        g_form.addOption('u_hr_location', 'Marion', 'Marion');
        g_form.addOption('u_hr_location', 'Marion MCP', 'Marion MCP');
    } else if (newValue == 'Lancaster') {
        g_form.clearOptions('u_hr_location', 'Location');
        g_form.addOption('u_hr_location', 'Chester', 'Chester');
        g_form.addOption('u_hr_location', 'Lancaster', 'Lancaster');
        g_form.addOption('u_hr_location', 'Lancaster MCP', 'Lancaster MCP');
    } else if (newValue == 'Midlands') {
        g_form.clearOptions('u_hr_location', 'Location');
        g_form.addOption('u_hr_location', 'Columbia', 'Columbia');
        g_form.addOption('u_hr_location', 'Kershaw', 'Kershaw');
        g_form.addOption('u_hr_location', 'Midlands MCP', 'Midlands MCP');
    }
}

Can it be that there's onLoad(), other script, or UI Policy setting Location drop down list? If so, deactivate them.

Sorry, but I can't replicate the problem of "location" drop down being filled with all locations when the form is loaded.

There isn't anything that I can see that would be changing it other than the two scripts I am working on.  I added in all the MCP options, so they couldn't be loading from any old scripts.

That is very close to what I want to do, except when loading if Charleston is the default (it is), I'd like the location to show both Charleston location options instead of being blank).

You not being able to reproduce it is where I'm confused, as that's pretty much the exact setup I have, except the division and location fields are string type with choice lists added (a relic from the previous admin)...and since I'm pretty new to service now I am a little hesitant to change the way the structure is.

The catalog item works perfectly as intended, it's when viewing the form in the backend that things aren't working as expected.

If I change the division field in the form it all fixes itself and works as intended, but on form load, the location field contains all locations instead of just the ones I want.  If I change the division, this fixes itself.  I'm going to play around with the onload script someone else mentioned and see if I can get that working to fix this issue hopefully.

find_real_file.png

Your script will get very difficult to manage at scale.

Your best option would be to create a custom u_hr_location table (use a lookup table so as not to affect licensing), with two columns: Division and Location.  Create a reference qualifier on the Location field

javascript: "division=" + <Name of division variable on form>

Once this is done, instead of having to go into the code every time a location/division relationship changes, you only have to update the table.  In addition, it will work both on Platform and Portal without any changes.