+ Start a Discussion
Sridhar BonagiriSridhar Bonagiri 

How to get the list of all fields and their datatypes of Objects

Hi,

 

How can I display all the fields and their datatypes of a particular object,

is there any code available to do this.

 

Thanks in advance.

SargeSarge

Hi,

 

  Use the following code:

 

<!--VF code-->
<apex:page controller="AccountFieldController">  
    <apex:form>
        <apex:pageBlock>
           <apex:dataList value="{!accountFieldResult}" var="af">
               <apex:outputText>{!af}</apex:outputText>
           </apex:dataList>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

 

 

/*Controller*/
public class AccountFieldController{

   public List<String> accountFieldResult {get; set;}
  
  public AccountFieldController(){
     Map<String, Schema.SObjectField> M = Schema.SObjectType.Account.fields.getMap();
     Set<String> fieldnames = M.keySet();         List<String> fieldList = new List<String>(); 
     fieldList.addAll(fieldNames);
     accountFieldResult = new List<String>();
       for(integer i=0; i<fieldList.size(); i++){
          accountFieldResult.add(fieldList[i]+':'+m.get(fieldList[i]).
                                 getDescribe().getType()); 
       }
   }
}

 

Hope this helps.