Dynamic Dropdown Menu

cc11
Tera Contributor

I have three fields as shows in the screenshot below,

find_real_file.png

1 User selects Category = 'Software'

2 On clicking Type dropdown, we want to query a Table called 'Inventory' (where we have Category, Type & Subtype columns data) and pull matching Type values from there.

But here is another thing - while displaying Type values, we need to transform them by appending Type & Subtype together with a dot(.) and making them lowercase.

So the Type dropdown should show the values in this format ->   type.subtype

Is this possible?

Thank you,

Yogesh

1 ACCEPTED SOLUTION

Hi Yogesh,



Please check below code with few modifications.



********** Script Include *****************


var DisplayTypeMenu = Class.create();


DisplayTypeMenu.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


getListDetails : function() {


var list = [];


var order = this.getParameter('sysparm_category');


var gr = new GlideRecord('x_137749_test_menuvalue'); // Table name was wrong, it had x_137749_test_menuvalues (extra s) instead of x_137749_test_menuvalue


gr.addQuery('localmenuname', order); // Field name was wrong, filed backend name is localmenuname


gr.query();


while(gr.next())


{


var item_val = gr.getDisplayValue('menuvalue');


list.push(item_val);


}


return JSON.stringify(list);


},


type: 'DisplayTypeMenu'


});



*********** Client Script ******************


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


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


return;


}


//g_form.clearOptions('type'); //change the field name with dependent field name


var listDetails = new GlideAjax("DisplayTypeMenu");


listDetails.addParam("sysparm_name", "getListDetails");


listDetails.addParam("sysparm_category", newValue);


listDetails.getXML(ajaxResponse);


function ajaxResponse(serverResponse) {


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


var myObj = JSON.parse(answer);


alert(myObj);


// if(myObj.length > 0)


// {


// for(var i = 0; i<myObj.length; i++)


// {


// //g_form.addOption('type', myObj[i].toLowerCase(), myObj[i].toLowerCase()); //Replace the field name where you have to assign the value


// alert(myObj[i].toLowerCase());


// }


// }


}


}



Result:


find_real_file.png


find_real_file.png


View solution in original post

39 REPLIES 39

please put alert inside for loop in client script and see what are you getting,



for(var i = 0; i<myObj.length; i++)


{


.//g_form.addOption('type', myObj[i].toLowerCase(), myObj[i].toLowerCase()); //Replace the field name where you have to assign the value


alert(myObj[i].toLowerCase());


}


I added the alert script but it doesnt seem to be executing. The code isnt making it to the for loop it seems


cc11
Tera Contributor

Thank you very much, Shishir,



I dont see any error anymore but the values arent coming.


I have a question -


1 Is sysparm_order in the line below specific to your form and I need to replace it with a column on my form OR is it a system constant?


var order = this.getParameter('sysparm_order');


2 Referring to the line below,


gr.addQuery('element', order); //Add your queries here



I am replacing it like this,


gr.addQuery('<backend form column from where the values will be fetched>', <current form variable on which the action is performed>);


like - gr.addQuery('inventory_cat', category);




No values are coming in Type dropdown:


find_real_file.png



Thanks again for your help,


Yogesh


cc11
Tera Contributor

I see that it does execute the line g_form.clearOptions('type'); and clears the dropdown.


So we know that it is triggering this server side code but for some reason its not making it to the for loop.



------------------


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


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


return;


}


g_form.clearOptions('type'); //change the field name with dependent field name


var listDetails = new GlideAjax("DisplayTypeList");


listDetails.addParam("sysparm_name", "getListDetails");


listDetails.addParam("sysparm_order", newValue);


listDetails.getXML(ajaxResponse);


function ajaxResponse(serverResponse) {


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


var myObj = JSON.parse(answer);


if(myObj.length > 0)


{


for(var i = 0; i<myObj.length; i++)


{


//g_form.addOption('type', myObj[i].toLowerCase(), myObj[i].toLowerCase()); //Replace the field name where you have to assign the value


alert(myObj[i].toLowerCase());


}


}


}


}


cc11
Tera Contributor

Sorry for sending multiple posts -



I tried to print the object as below. It is printing 'null' when I change the Category dropdown.


Is it because, Category dropdown on the front end & Inventory_Cat on the backend are Reference Variables and we need to treat them differently while doing a comparison?


find_real_file.png