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

patricklatella
Mega Sage

my "computer" field is mandatory, so I can't add "none" as a regular choice that can be selected.   I just need it to be the first visible option like it would be normally so the user has to make a selection.


Sorry for the delay. I will take a look at this thread in some time.


patricklatella
Mega Sage

thanks Pradeep,


here's my script and right now it's doing everything I need it to do except it is removing the "none" option that I want to display first as normal...and for "none" (as is OOTB) to not be a choice that can be selected.  




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


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


          return;


    }


if (newValue != oldValue) {


g_form.clearOptions('computer');


         


}


    //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'){


//alert('the region is '+answer);


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


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


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


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


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


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


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


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



}


if(answer == 'APAC'){


//alert('the region is '+answer);


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


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


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


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


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


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


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


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



}



if(answer == 'North America'){


//alert('the region is '+answer);


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


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


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


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


g_form.addOption('computer','financial_laptop','Financial Laptop',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','cad_17','Developer\CAD Laptop 17"',7);


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


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


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



}


}


patricklatella
Mega Sage

I guess I could add "None" as an option and then write an addition onSubmit script that blocks moving forward if "None" is chosen.



Is there a better way?


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);



}


}