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

not sure how I would make that work...what would that script look like?


patricklatella
Mega Sage

so here's my script.   It's working perfectly to removeOptions, just not sure how/where to put the script to put the options back at the top of the script



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


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


          return;


    }



//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.removeOption('computer','Game Developer Workstation');


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


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


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


}



if(answer == 'APAC'){


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


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


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


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


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


}


}


patricklatella
Mega Sage

is there something like "add all options back"?


patricklatella
Mega Sage

maybe getting close?   What I'm having it do here is put in all the possible choices that could have been removed.   but it's not working just yet




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


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


          return;


    }


if (newValue != oldValue){


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


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


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


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


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



}


patricklatella
Mega Sage

Hi Pradeep, still having a little trouble with the g_form.addOption and how to use it in my client script to put the choices back in if the "emp_location" changes.



Maybe the way to go is to have use g_form.clearOptions() at the beginning of the script and then to use g_form.addOptions based on the region returned in the Ajax call?   However I'm seeing that if I do this I lose the "none" option that I need to be included.   Is there a way to add "none" using addOption so if appears as normal for the field if I just have the choices and checked "include none"?