4

Can I return this data in any particular order?

@RemoteAction
public String unitList {
    get {
        return JSON.serializePretty(All_Data__c.getAll().values());
    }
}

Perhaps by using

ORDER BY

somehow and specifying a field?

I print it out here:

var unit_list_items = {!unitList};
var openUnitList = function (unitdiv) {
jQuery('div.unit_dropdown').remove();
var unit_ul = jQuery('<ul>'),
    unit_dr = jQuery('<div>')
        .addClass('unit_dropdown')
        .append(unit_ul);

unit_dr.appendTo(unitdiv);

jQuery.each(unit_list_items, function (idx, val) {
    unit_ul.append(jQuery('<li>')
                          .addClass('unit_list_items')
                          .text(val.Name));
});
};

But it seems to return in no particular order.

1 Answer 1

5

The getAll() method returns a map where the key is the setting name so to order by that:

public String planUnitList {
    get {
        List<String> names = new List<String>(All_Data__c.getAll().keySet());
        names.sort();
        List<All_Data__c> values = new List<All_Data__c>();
        for (String name : names) {
            values.add(All_Data__c.getAll().get(name));
        }
        return JSON.serializePretty(values);
    }
}

If you chose to use a SOQL query to get the custom settings rather than using the cached version as above, then yes you could use an order by in that query.

PS

The easiest way to sort by date (and do any other ordering or filtering by fields) is to use the query approach because then you have the full power of SOQL available. You lose the benefit of the custom setting caching but its only one extra query.

So then the code would look like this:

public String planUnitList {
    get {
        return JSON.serializePretty([
                select Name, DateField__c, ...
                from All_Data__c
                order by DateField__c
                ]);
    }
}

(To sort the getAll values you have to wrap the SObjects in a class that implements Comparable and sort those wrapper objects.)

3
  • Thanks Keith. Is there any way to sort by date perhaps? Using the above method?
    – Daft
    Commented May 21, 2014 at 10:00
  • @Daft I've added a PS to the answer.
    – Keith C
    Commented May 21, 2014 at 10:08
  • Brilliant! Cheers Keith! Unfortunately, I can only give the one upvote! :D Much appreciated man!
    – Daft
    Commented May 21, 2014 at 10:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .