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;
}else{
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.