Getting table back end values and putting them as Options for select box variable on the catalog form by writing script include and onChange client script
Hello All,
if you need to populate select box variable from a table, in this scenario below scripts will be helpful
ScriptInclude
var assetRequisitionUtils = Class.create();
assetRequisitionUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getBuildingNamesByCity : function(){ var buildins = []; var city = this.getParameter("sys_param_city"); var loc = new GlideRecord("u_geographical_hierarchy1"); loc.addQuery("u_city", city); loc.query(); while(loc.next()){ buildins.push(loc.u_building.toString()); } var dups = []; var arr = buildins.filter(function(el) { if (dups.indexOf(el) == -1) { dups.push(el); } }); return dups.toString(); }, getFloorByBuildings : function(){ var floors = []; var building = this.getParameter("sys_param_building"); var loc = new GlideRecord("u_geographical_hierarchy1"); loc.addQuery("u_building", building); loc.query(); while(loc.next()){ floors.push(loc.u_floor_name.toString()); } var dups = []; var arr = floors.filter(function(el) { if (dups.indexOf(el) == -1) { dups.push(el); } }); return dups.toString(); }, type: 'assetRequisitionUtils'
});
OnChange Client Script
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { g_form.clearOptions('floor_asset_requisition'); g_form.addOption('floor_asset_requisition', '', '--None--'); return; }var ga = new GlideAjax('assetRequisitionUtils');ga.addParam('sysparm_name', 'getFloorByBuildings');ga.addParam('sys_param_building', newValue);ga.getXML(HelloWorldParse); function HelloWorldParse(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); //alert(answer); var floors = answer.toString().trim().split(','); g_form.clearOptions('floor_asset_requisition'); g_form.addOption('floor_asset_requisition', '', '--None--'); for(var index = 0; index < floors.length; index++){ //alert(buildins[index]); //return; g_form.addOption('floor_asset_requisition', floors[index], floors[index]); }}
}
Thanks
https://www.servicenow.com/community/csm-articles/getting-table-back-end-values-and-putting-them-as-options-for/ta-p/2298978
