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

Sorry for the newbie question, but I'm still trying to learn javascript so this side of things is pretty new to me.  Would you be able to give me an example of how to write the onload script.  I have all the if logic from the onchange, but I'm not sure how to create the variables to choose which fields I want to edit.

Hey there!

 

Extend the IF condition in line 2

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

This is a sample script I have used to remove options onLoad. try to use it to tailor your script. 

function onLoad() {
  
	
    var businessService = g_form.getValue('u_business_service');
        
        //Business service = Amazon
        if (businessService != "c14dadc8db98741004b7e9ec0b9619bc") {
            
            g_form.removeOption('state', '-5');
        } else {
            
            g_form.addOption('state', '-5', 'Awaiting Change', 3);
        }
       
        
    }

hope this helps

please mark helpful 🙂

Yeah, please see if that solves your purpose else I will try to help you out with your script itself.

Shreya Kaushik1
Mega Guru

Hey Staxed,

Did you get a chance to try the "Dependent Field" option while creating the choice field? As it gives the user a direct option to declare the dependency of choice list values on another field's value.

Putting up an example from alm_asset table for your reference:

find_real_file.png

When you do this here in the dictionary entry itself, you can define the dependent value while creating the choice as follows:

find_real_file.png