I have a custom object that I need to select all fields from in a specific order to create a text file in a specific format. So far, I've been dynamically accessing all the fields I need via .getMap().

Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get('Custom_Object__c').getDescribe().fields.getMap();
        fieldLabels = objectFields.values();
        for(String fieldName : objectFields.keyset())
            if(commaSeparatedFields == null || commaSeparatedFields == ''){
                commaSeparatedFields = fieldName;
                commaSeparatedFields = commaSeparatedFields + ', ' + fieldName;
Database.getQueryLocator('select ' + commaSeparatedFields + ' from Custom_Object__c');

The problem is that the fields aren't in the order I want. I could of course hard-code the field names into the query, but the second one field changes, etc. my code will break.

Any suggestions of how to specify field order some other way? I was thinking if I could add custom metadata to each field and give them a priority to order by, then I could maybe access that metadata when compiling them into 'commaSeparatedFields' and then be able to order them dynamically. Not sure how to do this though. Any suggestions are appreciated.

  • You can select them in any order. When you generate your file, you can rearrange the fields as needed based on their names. You have most of this already done via describe/metadata
    – identigral
    Jan 28, 2021 at 18:18
  • It's true I can rearrange the order after they are selected, but then I'll still have to hard-code each field name to put them in order. I'm trying to avoid the hard-coding piece.
    – sFishman
    Jan 31, 2021 at 9:01
  • You don't have to hard-code anything. You need some way of specifying your target format, custom metadata is an option or just using a custom object of your own. Select in any order, then render CSV by reading your format specification and rearrange the fields. Your question is an example of XY problem
    – identigral
    Jan 31, 2021 at 17:28

1 Answer 1


In the past I have used FieldSet for similar requirements but now that I check again the order is ensured in Visualforce but not explicitly ensured in Apex.

Alternatively, one can implement this with a new Custom Metadata Type. The order can be set via a field and you query in your code to retrieve them in the defined order. Additionally Custom Metadata records can be maintained in your code repository and they can be deployed together with your code.

There's a cool Trailhead explaining more details on how to use them

  • Not sure this addresses my need. Custom Metadata Types can help with ordering RECORDS. I'm trying to order my FIELDS for each record in a specific way.
    – sFishman
    Jan 31, 2021 at 9:00
  • The custom metadata will contain 2 fields (or more based on your requirements): Order, FieldName. Then you will query the metadata ordered by the Order and you will have the Field Names (api recommended) in the desired order.
    – ManSpan
    Feb 1, 2021 at 9:59

You must log in to answer this question.

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