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

Yes, the script include - client callable is checked.



Thank you,


Yogesh


Hi Shishir,



I have uploaded the application here Application for troubleshooting Context Driven Menu



The zip file has the application file (.xml) and 3 csv files, one for each form in the application.



Here is a screenshot of the Menus. The goal is to select Category dropdown (say 'Software') from the TCase form below.


find_real_file.png



and then match the above Category of 'Software' with 'LocalMenuValue' from the Table below.   And so, the Type would show the value 'SQL' in this case.


find_real_file.png



I have written the ClientScript & Script Include on the TCase form.


Let me know for any question.


antin_s
ServiceNow Employee
ServiceNow Employee

Hi Yogesh,



Is it a possibility to have the Inventory values in Choice (sys_choice) table? Choice table can solve your use case easily with just configuration and without any code. All you need to do is create the choice list and dependancies.



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


cc11
Tera Contributor

Thanks Antin,



The intent is to create every possible menu on the application that can be configured by changing values from two Configuration Forms. Say, ManuName & ManuValue (one-to-many carnality).   The MenuName form will be visible to BusinessAdmin who will be able to Add/Update Menu & its values.



Thank you,


Yogesh


antin_s
ServiceNow Employee
ServiceNow Employee

Yes, this can be achieved by creating choice list and enabling access to your Business Admin to have access to only certain records in Choice table.