function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Walter@AdicioWalter@Adicio 

How do I pull field labels instead of field names out of the describe results for an object?

This is what I have right now, but I want the select options list to show the field label to the user and pass the field name when selected by the user.

 

Schema.DescribeSObjectResult obj = Quote_Comment__c.sObjectType.getDescribe(); Map<String, Schema.SObjectField> objFields = obj.fields.getMap(); for( String fieldName : objFields.keySet() ) { orderByOptions.add(new SelectOption(fieldName,fieldName )); }

 

Best Answer chosen by Admin (Salesforce Developers) 
sornasorna

Schema.SobjectField can be converted to describefieldResult. Then using describeFieldResult.getLabel(), you can get the field labels. In your case:

 for(String fieldName : objFields.keySet()){

       Schema.SobjectField s = objFields.get(fieldname);

       DescribeFieldResult f = s.getDescribe();

       orderByOptions.add(new SelectOption(fieldName,f.getlabel()));

}

All Answers

sornasorna

Schema.SobjectField can be converted to describefieldResult. Then using describeFieldResult.getLabel(), you can get the field labels. In your case:

 for(String fieldName : objFields.keySet()){

       Schema.SobjectField s = objFields.get(fieldname);

       DescribeFieldResult f = s.getDescribe();

       orderByOptions.add(new SelectOption(fieldName,f.getlabel()));

}

This was selected as the best answer
Walter@AdicioWalter@Adicio
Thank you sorna.
EvaDEvaD

Is there anyway to get around the limit in field describes?  I'm using something similar, but I have to put a limit of 10 otherwise we have errors about too many field describes. Excerpt from the code, getting Account History fields:

 

 

   
String fieldName;
        fieldName = String.valueOf(fh.Field);
        
            Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Account.fields.getMap();
        
        System.debug('FIELD: ' + fieldName);
        for(String gfieldName:fieldMap.keySet()) {
        System.debug('gField: ' + gfieldName);
        if(gFieldName == fieldName){
            Schema.DescribeFieldResult fieldDescribe = fieldMap.get(gfieldName).getDescribe();
         fieldName = fieldDescribe.getLabel();
        }
        }

 

 

Any insight would be appreciated greatly!

 

- Eva