Urgent Help needed for sorting a select box variable

muktha1
Tera Guru

I have created a lookup select box variable in a record producer which relates to a choice field on the corresponding table. I would like to sort the choices on the variable to match the order on the choice field in the table. The choices get  arranged in alphabetical order in the variable. The choice table is a custom table with a choice field also.  How do I  sort the variable choices in the same order of the choice field in the table?

 find_real_file.png

 

Thanks in advance

4 REPLIES 4

Mohit Kaushik
Mega Sage
Mega Sage

Hi you can set the sequence value as per the order you have in choice list and then it should reflect in the same order as you want it instead of alphabetical order. Hope this will help.

Please mark this answer correct if it resolved the query and mark it helpful if it helped you at all

Thanks,

Mohit Kaushik

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Hi Mohit,

 

Thanks for your reply. But though the sequence is set in the table choice field, it is not reflected in the variable when it gets displayed in the service portal. It shows the choices in the alphabetical order. For example, I have given the option 'other' as the last in the sequence in the choice field. But in the select variable it gets displayed in the middle as per alphabetical order.

Hi Muktha,

In this case you need to write a Reference qualifier in your lookup select box. In the qualifier you can filter it out as per sequence.

Please follow the below threads for using the reference qualifier. Hope this will help you.

https://community.servicenow.com/community?id=community_question&sys_id=7d93c765dbd8dbc01dcaf3231f96...

https://docs.servicenow.com/bundle/jakarta-it-service-management/page/product/service-catalog-manage...

 

Please mark this answer correct if it resolved the query and mark it helpful if it helped you at all

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Michael Jones -
Giga Sage

As Mohit suggests, this should just work out of the box. If the field on your table is of a type "choice" and you have configured the choices on that field on the table, when you link that field to a select box then you should see the same options, in the same order. 

You can try setting the options the "hard way" and see if you get a different result, but it will take a little additional work. 

This all assumes that you have a custom table, but you are using the built-in field type of Choice, with configured choices in the sys_choice table!

First, you need a GlideAjax Script include, client-callable, to do the work server side. 

Name it sortSelectBox

Here is the code: 

var sortSelectBox = Class.create();
sortSelectBox.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	type: 'sortSelectBox',
	getChoices: function() {
		
		//Queries the Choice Table and returns the options, including sequence.
		var table = this.getParameter('sysparm_table_name');
		var field = this.getParameter('sysparm_table_field');
		var includeNone = this.getParameter('sysparm_include_none');
		var padIndex = 0;
		var currIndex = 0;
		if(includeNone == 'true') {
			padIndex = 1;
		}
		
		var qStr = "nameINjavascript:getTableExtensions('" + table + "')^element=" + field;
		var retStr = [];
		var jStr = '[';
		var gr_choice = new GlideRecord('sys_choice');
		gr_choice.addEncodedQuery(qStr);
		gr_choice.orderBy('sequence');
		gr_choice.query();
		while (gr_choice.next()) {
			currIndex = gr_choice.sequence + padIndex;
			jStr += '{"label":"' + gr_choice.getValue('label') + '","value":"' + gr_choice.getValue('value') + '","index":"' + currIndex + '"},';
			}
			jStr = jStr.substring(0, jStr.length - 1);
			jStr += "]";
			return jStr;
		}
	});

This will take two input parameters, the table name and the field on the table. It will then query the sys_choice table and return the options that have been configured, along with the sequence, and return that data as a JSON object. 

You would call this in a client script, onload, using this format (replace values in <> with the ones correct for you. 

    var ga = new GlideAjax('sortSelectBox');

    ga.addParam('sysparm_name', 'getChoices');
    ga.addParam('sysparm_table_name', '<name of table for choice field>');
    ga.addParam('sysparm_table_field', '<name of choice field on table>');
    //I assume you have included none on the select box, if not change to false
    ga.addParam('sysparm_include_none', 'true');
    ga.getXML(ajProcessor);
	

    function ajProcessor(response) {
        var fieldName = '<name of your field>'; 
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var obj = JSON.parse(answer);
        var i;
        for (i = 0; i < obj.length; i++) {
            g_form.addOption(fieldName, obj[i].value, obj[i].label, obj[i].index);
            
        }
    }

 

If you try the above and you still get options in the wrong order, then you might need to confirm directly on the sys_choice table that the options have the sequence that you are expecting. If you are not using the sys_choice table for some reason, you would have to modify the Ajax call to hit the right table, etc. 

Hope this helps! 

 

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the Cloudpires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!