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
anjin reddy 9anjin reddy 9 

Getting all fields&Labels of objects in one table

Hi Friends 

Could you please suggest me that
   How to get the all fields name and field label names of objects in One table 
 
Best Answer chosen by anjin reddy 9
Abhishek BansalAbhishek Bansal
Hi Anjin,

In order to display filed data type along with label and names, you have to modify your VF page as well as controller class.

Please find below the update VF page and class :

VF Page :
<apex:page controller="TestingController">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!mapOfFieldNameWithFieldLabel}" var="accName">
            <apex:column headerValue="Account Field API Name" value="{!accName.Name}"/>
            <apex:repeat value="{!accName.outers}" var="accLabel">
                <apex:column headerValue="Account Field Label" value="{!accLabel}"/>
                <apex:column headerValue="Account Field Data Type" value="{!accName.outers[accLabel]}"/>
            </apex:repeat> 
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Controller class :
public class TestingController {
    public List<innerClass> mapOfFieldNameWithFieldLabel {get;set;}
   
    public TestingController(){
        //Below is the sample code for getting all field names and labels for Account Object. Replacing this Account name you can get field names and labels for any other object

        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        
        Schema.SObjectType accSchema = schemaMap.get('Account');
        
        Map<String, Schema.SObjectField> fieldMap = accSchema.getDescribe().fields.getMap();
        
        mapOfFieldNameWithFieldLabel = new List<innerClass>();
        Map<String,Schema.DisplayType> dataTypeMap;
        for (String fieldName: fieldMap.keySet()) {
            dataTypeMap= new Map<String,Schema.DisplayType>();
            
            dataTypeMap.put(fieldMap.get(fieldName).getDescribe().getLabel(),fieldMap.get(fieldName).getDescribe().getType());
            mapOfFieldNameWithFieldLabel.add(new innerClass(fieldName,dataTypeMap));
            
        }
        //Now this map will hold all the Field Name with their labels for Account Obejcts
    }
    
    public class innerClass
    {
        public String Name {get;set;}
        public Map<String,Schema.DisplayType> outers {get;set;}

        public innerClass(String Name, Map<String,Schema.DisplayType> outers)
        {
            this.Name = Name;
            this.outers = outers;
        }
    }
}
Replace your code with above code.
Let me know if you have any further issues.

Regards,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi,

I have provided below a sample code to get all fields and labels of Account Object :
//Below is the sample code for getting all field names and labels for Account Object. Replacing this Account name //you can get field names and labels for any other object

Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

Schema.SObjectType accSchema = schemaMap.get('Account');

Map<String, Schema.SObjectField> fieldMap = accSchema.getDescribe().fields.getMap();

Map<String,String> mapOfFieldNameWithFieldLabel = new Map<String,String>();

for (String fieldName: fieldMap.keySet()) {
	mapOfFieldNameWithFieldLabel.put(fieldName,fieldMap.get(fieldName).getDescribe().getLabel());
}
//Now this map will hold all the Field Name with their labels for Account Obejcts

In the same way you can get all fields and labels of other objects also.

Let me know if you need more help on this.

Regards,
Abhishek Bansal.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you
<apex:page controller="ExtractSobject">
<apex:form >
   <apex:pageblock >
     <apex:pageblocksection >
       <apex:pageBlockSectionItem >
         <apex:outputlabel value="Select Object"/>
          <apex:selectList value="{!Selectedobject }" size="1">
            <apex:selectoptions value="{!Selectedobjnames}"></apex:selectoptions>
            <apex:actionSupport event="onchange" rerender="a"/>
         </apex:selectList>
       </apex:pageBlockSectionItem>
      
     <apex:pageBlockSectionItem >
      <apex:outputPanel id="a">
         <apex:outputLabel value="Object Fields" ></apex:outputLabel>
             <apex:selectList value="{!Sf}" size="1"> 
                   <apex:selectOptions value="{!SelectedobjFields}" />
             </apex:selectList>
      </apex:outputPanel>
     </apex:pageBlockSectionItem>
     
     
   </apex:pageBlockSection>   
  </apex:pageblock>
 </apex:form>
 
</apex:page>
public with sharing class ExtractSobject
{
    public String Sf{get;set;}
    public String Selectedobject { get; set; }    
  
        public List<SelectOption> getSelectedobjnames()
        {
            List<Schema.SObjectType> obj = Schema.getGlobalDescribe().Values();
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('--Select Object--','--Select Object--'));
            for(Schema.SObjectType st : obj)
            {
                if(Selectedobject == null || Selectedobject=='' )
                {       
                    Selectedobject = st.getDescribe().getName();
                }
                options.add(new SelectOption(st.getDescribe().getName(),st.getDescribe().getName()));
            }
            return options;
        }
       
        public List<SelectOption> getSelectedobjFields()
        {
            SObjectType objTyp = Schema.getGlobalDescribe().get(Selectedobject);
            DescribeSObjectResult objDef = objTyp.getDescribe();
            Map<String, SObjectField> fields = objDef.fields.getMap();

            Set<String> fieldSet = fields.keySet();
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('--Select Object--','--Select Object--'));
            for(String s:fieldSet)
            {  
                SObjectField Sobjfields = fields.get(s);
                DescribeFieldResult selectedField = Sobjfields.getDescribe();                 
                options.add(new SelectOption(selectedField.getName(),selectedField.getName()));
            }
            return options;
        }
}
Please let us know if this will help you

Thanks
Amit Chaudhary


 
anjin reddy 9anjin reddy 9
Hi Abishek
 Tanks for suggest
  I need to display all field names and label in one table ..Could please suggest me
anjin reddy 9anjin reddy 9
Hi Amit Choudary 

  please try to give to display both fields and labels of any object in one table 
Abhishek BansalAbhishek Bansal
Hi Anjin,

In order to show the names and labels in a single table you have to create a VF page and its Controller class.
I am providing you the code below for displaying fields related to Account.

Create a new class "TestingController" and add below mentioned Code in it :
 
public class TestingController {
    public Map<String,String> mapOfFieldNameWithFieldLabel {get;set;}
    public TestingController(){
        //Below is the sample code for getting all field names and labels for Account Object. Replacing this Account name //you can get field names and labels for any other object

        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        
        Schema.SObjectType accSchema = schemaMap.get('Account');
        
        Map<String, Schema.SObjectField> fieldMap = accSchema.getDescribe().fields.getMap();
        
        mapOfFieldNameWithFieldLabel = new Map<String,String>();
        
        for (String fieldName: fieldMap.keySet()) {
            mapOfFieldNameWithFieldLabel.put(fieldName,fieldMap.get(fieldName).getDescribe().getLabel());
        }
        //Now this map will hold all the Field Name with their labels for Account Obejcts
    }
}
Now create a new VF page and add below mentioned code in it. :
 
<apex:page controller="TestingController">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!mapOfFieldNameWithFieldLabel}" var="accName">
            <apex:column headerValue="Account Field API Name" value="{!accName}"/>
            <apex:column headerValue="Account Field Label" value="{!mapOfFieldNameWithFieldLabel[accName]}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Now save the class and VF page.
When you click on preview button on VF page thn you will see a table with all fields and lables of account in one table.
Please see below screenshot :
User-added image

Please modify your code if you want to show fileds for any other object.
Let me know if you need more help on this.

Regards,
Abhishek.

 
anjin reddy 9anjin reddy 9
Hi Abhishek,
 
  Thanks You so much 
  I got the Answer What Am expecting   
  As well as I need some more information that i need data types along with fields name
 Can you please suggest me .Am waitmg for your answer

Thanks
 
Abhishek BansalAbhishek Bansal
Hi Anjin,

In order to display filed data type along with label and names, you have to modify your VF page as well as controller class.

Please find below the update VF page and class :

VF Page :
<apex:page controller="TestingController">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!mapOfFieldNameWithFieldLabel}" var="accName">
            <apex:column headerValue="Account Field API Name" value="{!accName.Name}"/>
            <apex:repeat value="{!accName.outers}" var="accLabel">
                <apex:column headerValue="Account Field Label" value="{!accLabel}"/>
                <apex:column headerValue="Account Field Data Type" value="{!accName.outers[accLabel]}"/>
            </apex:repeat> 
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Controller class :
public class TestingController {
    public List<innerClass> mapOfFieldNameWithFieldLabel {get;set;}
   
    public TestingController(){
        //Below is the sample code for getting all field names and labels for Account Object. Replacing this Account name you can get field names and labels for any other object

        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        
        Schema.SObjectType accSchema = schemaMap.get('Account');
        
        Map<String, Schema.SObjectField> fieldMap = accSchema.getDescribe().fields.getMap();
        
        mapOfFieldNameWithFieldLabel = new List<innerClass>();
        Map<String,Schema.DisplayType> dataTypeMap;
        for (String fieldName: fieldMap.keySet()) {
            dataTypeMap= new Map<String,Schema.DisplayType>();
            
            dataTypeMap.put(fieldMap.get(fieldName).getDescribe().getLabel(),fieldMap.get(fieldName).getDescribe().getType());
            mapOfFieldNameWithFieldLabel.add(new innerClass(fieldName,dataTypeMap));
            
        }
        //Now this map will hold all the Field Name with their labels for Account Obejcts
    }
    
    public class innerClass
    {
        public String Name {get;set;}
        public Map<String,Schema.DisplayType> outers {get;set;}

        public innerClass(String Name, Map<String,Schema.DisplayType> outers)
        {
            this.Name = Name;
            this.outers = outers;
        }
    }
}
Replace your code with above code.
Let me know if you have any further issues.

Regards,
Abhishek
This was selected as the best answer
anjin reddy 9anjin reddy 9
HI Abhishek,
Thank You so much ,,The Code which you send is very useful for us .

1) Issue is that ..starting letter of API Name is "CapitalLetter"..Its Genarating As "SmallLetter"
2)Is it Possible to generate the which its displyed in Org.

Plz Correct and resend me please

Thanks
 
 
Abhishek BansalAbhishek Bansal
Hi Anjin,

This is the default behavior, and no there's no "direct" workaround. 
Please find the reason for same below :



The reason why is because the Map returned by the call is actually a special map that I'll call CaseInsensitiveKeyMap. A normal map would more appropriately be called a CaseSensitiveKeyMap, because "Hello" and "hello" are two unique keys (because they use hashCode(), which will definitely return unique values for even a change in string case).

On the other hand, the describe calls, such as fields.getMap() and fieldSets.getMap(), effectively call String.toLowerCase() on each key before calling hashCode(), such that you can ask for Name, NAME, or name (or any other permutation) to get the correct field. Field names are case insensitive in Apex Code, and this map mimics that behavior.

If you absolutely wanted to have a case sensitive map, you'd have to construct your own Map, iterate through the values from the describe call, and set each key individually. This would be a large waste of CPU time for no real benefit, since there shouldn't be any specific reason why you need to have the correct casing.

Regards,
Abhishek
anjin reddy 9anjin reddy 9
Hello Abhishek,

   Thanks you so much for your suggest.My Solved

Thanks
Anjin
 
anjin reddy 9anjin reddy 9
Hi Abhishek,

 I just need fields , label and datatype of particulor object But not its child fields ,
2)The code which you send is extra fields .
  so pleases once check and let me know plz
Thanks
anjin reddy 9anjin reddy 9
HiAbhishek,

  Code Which you drop is genearting extra fileds can u plz check and let me know plzzzz
Abhishek BansalAbhishek Bansal
Hi Anjin, As this is not the same question for which you have asked for help so i would suggest you to open a new question and ask the same problem there. It will be good for you and others to find different solutions of different questions on different forums. So pelase explain your issue in more detail on another forum and send me the link of that new question . I will surely look into it and if possible, I will come up with a solution for you as soon as i can. In future please take it as a habit and do not mix up the questions in single forum :) Regards, Abhishek
anjin reddy 9anjin reddy 9
Thank you Abhishek
anjin reddy 9anjin reddy 9
Hi Abhishek,

  Thank you for giving valuble code,
 
 Can you please give to get the discription of each filds in table formate ..This will be very useful for us .

Thanks
Anjin