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

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Patrick,



I think there is no issue with removal of choice form select box. Can you please share your script so that we can take a look.


patricklatella
Mega Sage

Hi Pradeep,


thanks for the reply.   Here's my script include so far.   Still working on my client script, any help would be great.   I'll reply with what I have so far in a couple minutes





getRegion: function(){//this function is to determine which computers need to be removed from the choice list



var retVal; // Return value


var location     = this.getParameter('sysparm_location');



var RegionRec   = new GlideRecord('cmn_location');//table where desired variable lives


RegionRec.addQuery('sys_id',region);


RegionRec.query();


// Query user records


if(RegionRec.next())


{



retVal = RegionRec.u_region;//name of field with info you want to grab


}



return retVal;



}


Sure. In the meanwhile adjust the line RegionRec.addQuery('sys_id',region); to RegionRec.addQuery('sys_id',location);


patricklatella
Mega Sage

great thanks.   So here's my client script so far.



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_user', 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' = 'APAC';


g_form.removeOption('computer',Workstation);//Workstation is the choice I want to remove





}