+ Start a Discussion
adamproadampro 

Retrieving Fields from Objects

I'm currently writing a Visualforce page that prompts the user to select an object in their org, and then choose a field from that object. I've found a way to compile the list of objects without any trouble, but I'm struggling to compile a list of the fields belonging to that object. When the user selects an object, I store the object name as a string. I can find the fields for a designated object using the code below:

 

Map<String, Schema.SobjectField> M = Schema.SObjectType.Account.fields.getMap();
            
List<Schema.SObjectField> resultList = M.values();

 

However, I can't seem to use that code to display the fields of every object. The object name goes where it says "Account" but when I try using the string name for the object that was selected, it throws me an error message. I can't find a way to reference any object without manually typing the name in. I know I need to either convert the string name to some other sort of data type or come up with a new algorithm altogether.

 

Does any one have any suggestions?

Best Answer chosen by Admin (Salesforce Developers) 
WizradWizrad

Hey Adam,

 

You're over thinking this.

 

String selectedObject = 'Account';
Map<String, Schema.SObjectType> gdMap = Schema.getGlobalDescribe();
Schema.Describesobjectresult dsr = gdMap.get(selectedObject).getDescribe();
Map<String, Schema.SObjectField> fieldMap = dsr.fields.getMap();

/* Lets build a select list full of all fields for this object.  
   Let's also be careful because there is a limit of 100 describe 
   calls per transaction...this code wont hit that unless you do field describes to get the
   labels. */

List<SelectOption> soList = new List<SelectOption>();
for(String key : fieldMap.keySet()) {
  soList.add(new SelectOption(key, key.replace('__c', '').replace('_', ' '));
}

 

All Answers

WizradWizrad

Hey Adam,

 

You're over thinking this.

 

String selectedObject = 'Account';
Map<String, Schema.SObjectType> gdMap = Schema.getGlobalDescribe();
Schema.Describesobjectresult dsr = gdMap.get(selectedObject).getDescribe();
Map<String, Schema.SObjectField> fieldMap = dsr.fields.getMap();

/* Lets build a select list full of all fields for this object.  
   Let's also be careful because there is a limit of 100 describe 
   calls per transaction...this code wont hit that unless you do field describes to get the
   labels. */

List<SelectOption> soList = new List<SelectOption>();
for(String key : fieldMap.keySet()) {
  soList.add(new SelectOption(key, key.replace('__c', '').replace('_', ' '));
}

 

This was selected as the best answer
adamproadampro

Thank you both for your responses. Both examples helped me very much.

Mohit KapoorMohit Kapoor
Hi, Sorry to ask a doubt on this as this is really old, but I used this logic in one of my codes and I am currently facing an issue as this logic works perfectly for custom fields but when we call a standard field, as it does not have __c it causes an error. 

Have you guys ever encountered this or if you have any solution for this, it would be really helpful. 

Thanks in advance