Script Include: Return results sorted alphabetically

JuliaHowells
Tera Expert

We currently have a UI Action that calls a UI Macro/ UI Page and includes a script include. I want the returned names to display alphabetically by first name, then last name. Ideas on how to implement? 

 

Script Include: 

var SoftwareAjax = Class.create();
SoftwareAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getSoftwareOptions: function() {
        var result = [];
        
        // Query the 'alm_license' table for available software records
        var gr = new GlideRecord('alm_license');
        gr.addQuery('active', true);  // Add additional filters as needed
        gr.query();
        
        // Loop through the results and push them into the result array
        while (gr.next()) {
            result.push({
                value: gr.getValue('sys_id'),
                text: gr.getValue('name')  // Replace 'name' with the appropriate field
            });
        }
        
        // Log the result for debugging
        gs.log("SoftwareAjax - Retrieved Software Options: " + JSON.stringify(result));
        
        // Return the result as a JSON string
        return JSON.stringify(result);
    },

    type: 'SoftwareAjax'
});

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Julia,

You can sort the array before or after returning it, but in this example you could also add a line to the GlideRecord before the gr.query();

gr.orderBy('name');

or whatever field(s) are on the alm_license records that contain a first name and last name.

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Hi Julia,

You can sort the array before or after returning it, but in this example you could also add a line to the GlideRecord before the gr.query();

gr.orderBy('name');

or whatever field(s) are on the alm_license records that contain a first name and last name.

Thank you! I was way overcomplicating that. 

You are welcome!

 

 

Connect with me https://www.linkedin.com/in/brad-bowman-321b1567/