g_form.removeOption for setting choices in a select box

patricklatella
Mega Sage

Hi gang,

I'm looking to use an onChange client script and a script include to do the following:

- when the "location" field is set on a form I need the onChange client script/script inlude to lookup the 'region' of the entered 'location' from our location table.  

- then based on that location I want to remove certain options from a (separate) select box variable on the form.

I'm seeing a little chatter on the community that using g_form.removeOption may not work on a catalog item?   This is actually going to be on the "Describe Needs" (first page) of an order guide.

Does anyone know if this is possible, whether there's a better way, and can help with the script?

thanks!

1 ACCEPTED SOLUTION

patricklatella
Mega Sage

OK I got it...here's my script.   It first removes all the options, and then adds back what is needed depending on the answer from the Ajax call.   Thanks for your input Pradeep!



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


if (newValue != oldValue) {


g_form.removeOption('computer','no_computer');


g_form.removeOption('computer','existing_computer');


g_form.removeOption('computer','standard_laptop');  


g_form.removeOption('computer','Standard Desktop');  


g_form.removeOption('computer','financial_laptop');  


g_form.removeOption('computer','executive_laptop');  


g_form.removeOption('computer','cad_15');


g_form.removeOption('computer','cad_17');


g_form.removeOption('computer','Developer Workstation');


g_form.removeOption('computer','Game Developer Workstation');


g_form.removeOption('computer','CAD Workstation 1');


g_form.removeOption('computer','CAD Workstation 2');



}


    //Type appropriate comment here, and begin script below


    var ga = new GlideAjax('u_New_Hire_Scripts_Ajax');//name of script include


ga.addParam('sysparm_name', 'getRegion');//name of function on script include


ga.addParam('sysparm_location', g_form.getValue('emp_location'));//name of field on form triggering call


ga.getXML(EmployeeRegionLookup); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)


}



// Callback function to process the response returned from the server


function EmployeeRegionLookup(response) {


var answer = response.responseXML.documentElement.getAttribute("answer");


if(answer == 'EMEA'){


g_form.addOption('computer','no_computer','No computer required',1);


g_form.addOption('computer','existing_computer','Existing shared computer',2);


g_form.addOption('computer','standard_laptop','Standard Laptop',3);


g_form.addOption('computer','Standard Desktop','Standard Desktop',4);


g_form.addOption('computer','executive_laptop','Executive Laptop',5);


g_form.addOption('computer','cad_15','Developer\CAD Laptop 15"',6);


g_form.addOption('computer','Developer Workstation','Developer Workstation',7);


g_form.addOption('computer','CAD Workstation 1','CAD Workstation 1',8);



}


if(answer == 'APAC'){


g_form.addOption('computer','no_computer','No computer required',1);


g_form.addOption('computer','existing_computer','Existing shared computer',2);


g_form.addOption('computer','standard_laptop','Standard Laptop',3);


g_form.addOption('computer','Standard Desktop','Standard Desktop',4);


g_form.addOption('computer','financial_laptop','Financial Laptop',5);


g_form.addOption('computer','executive_laptop','Executive Laptop',6);


g_form.addOption('computer','cad_15','Developer\CAD Laptop 15"',7);


g_form.addOption('computer','Developer Workstation','Developer Workstation',8);



}



if(answer == 'North America'){


g_form.addOption('computer','no_computer','No computer required',1);


g_form.addOption('computer','existing_computer','Existing shared computer',2);


g_form.addOption('computer','standard_laptop','Standard Laptop',3);


g_form.addOption('computer','Standard Desktop','Standard Desktop',4);


g_form.addOption('computer','financial_laptop','Financial Laptop',5);


g_form.addOption('computer','executive_laptop','Executive Laptop',6);


g_form.addOption('computer','cad_15','Developer\CAD Laptop 15"',7);


g_form.addOption('computer','cad_17','Developer\CAD Laptop 17"',8);


g_form.addOption('computer','Developer Workstation','Developer Workstation',9);


g_form.addOption('computer','Game Developer Workstation','Game Developer Workstation',10);


g_form.addOption('computer','CAD Workstation 1','CAD Workstation 1',11);


g_form.addOption('computer','CAD Workstation 2','CAD Workstation 2',12);



}


}


View solution in original post

24 REPLIES 24

Is region field a reference field?


patricklatella
Mega Sage

found it needed to change this line in my client script:



ga.addParam('sysparm_location', g_form.getValue('emp_location'));//name of field on form triggering call



now checking to see if the choices was taken off the list.



find_real_file.png


patricklatella
Mega Sage

but not for North America, other regions


patricklatella
Mega Sage

excellent, it's working!   Still going to add some more removeOptions, but also need to have a way if the "emp_location" changes again and choices need to get put back in the "computer" field.   What can I add to achieve that?


You can keep a check to clear and add all the options back if the oldValue is not equal to newValue via g_form.addOption