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
krishna guptakrishna gupta 

how to display all sobject and their fields?

in one drop down, i want to display all object, once i will select the object from dropdown, in second dropdown it will show all the fields for selected objects.
Best Answer chosen by krishna gupta
Swarvi KumariSwarvi Kumari
Hi krishna,


    Once try this code.

    Visualforce page :
<apex:page controller="DynamicTableController123">
    <apex:form >
    
            <apex:actionFunction name="ObjectFileds" action="{!ObjectFields}"/>
<APEX:PAgeBlock >
     <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Select Object"/>
                <apex:selectList multiselect="false" size="1" value="{!SelectedObject}" onchange="ObjectFileds();">
                    <apex:selectOption itemLabel="--None--" itemValue="--None--"/>
                    <apex:selectoptions value="{!supportedObject}" />
                </apex:selectlist>
            </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Select Field"/>
                <apex:selectList multiselect="true" size="5" value="{!SelectedFields}">
                    <apex:selectOption itemLabel="--None--" itemValue="--None--"/>
                    <apex:selectoptions value="{!fieldLableAPI}" />
                </apex:selectlist>
            </apex:pageBlockSectionItem>
    
    
    
    </apex:pageblocksection>
    
    
    </APEX:PAgeBlock>
    
    
        </apex:form>
        </apex:page>

Controller :
public class DynamicTableController123 {

    public String SelectedObject {get; set;}
    public List<SelectOption> fieldLableAPI {get; set;}
    public List<selectoption> supportedObject {get; set;}
    public List<String> SelectedFields {get; set;}
    public List<sObject> ObjectList {get; set;}

 public DynamicTableController123 ()
    {
        //Initialize
        supportedObject = new List<selectoption>() ;
        SelectedObject = '' ;
        fieldLableAPI = new List<SelectOption>() ;
        SelectedFields = new List<String>() ;
        ObjectList = new List<sObject>() ;
        
        //Get only reference to objects
        for(Schema.SObjectType item : ProcessInstance.TargetObjectId.getDescribe().getReferenceTo())
        {
            //Excluding custom setting objects
            if(!item.getDescribe().CustomSetting)
            {
                //Adding to list
                supportedObject.add(new SelectOption(item.getDescribe().getLocalName().toLowerCase() , item.getDescribe().getLabel() ));
            }
        }
        
    }
    


 Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
    Set<String> objectKeys = gd.keySet();
 public void ObjectFields()
    {
        if(SelectedObject != '--None--')
        {
            //Creating sObject for dynamic selected object
            Schema.SObjectType systemObjectType = gd.get(SelectedObject);
            //Fetching field results
            Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
                
            Map<String, Schema.SObjectField> M = r.fields.getMap();
            //Creating picklist of fields
            for(Schema.SObjectField fieldAPI : M.values())
            {
                fieldLableAPI.add(new SelectOption(fieldAPI.getDescribe().getName() , fieldAPI.getDescribe().getLabel())) ;
            }
        }
    }

}

 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

There is no spacific Query to list all the fiedls for all SObject. you have to use any app to schema lister to view all the field bassed on object. 

You can try Schema Lister developed by one of the developers at Tquila. Navigate to URL https://schemalister.herokuapp.com/, select your environment and API version, authorise the Apps through OAuth and it will generate a list of all the objects and fields. The benefit of using this app is that you don’t need to install anything in your Salesforce org and don’t need to provide your Salesforce username and password (as the app uses OAuth to connect to Salesforce)

if you are not convinenet using above said link find  some apps which is avaiable in App Exchange. 

https://appexchange.salesforce.com/results?keywords=metadata

User-added image

Hope this will help you, 

mark Best ANSWER if its clear. 

Thanks
karthik
 
Naveen DhanarajNaveen Dhanaraj

1. Retrieve the objects from the salesforce Org.
Display all the Custom objects with “__C”  names in a picklist field:

page:
=====
<apex:page controller="ObjectRetrieve">
<apex:form >
<apex:outputlabel value="All Objects"/>&nbsp;&nbsp;
<apex:selectList size="1">
<apex:selectoptions value="{!objnames}"></apex:selectoptions>
</apex:selectList>
</apex:form>
</apex:page>



class:
======
public with sharing class ObjectRetrieve {

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

public List<SelectOption> getobjNames()
{
List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('--None','--None--'));
for(Schema.SObjectType f : gd)
{
if(f.getDescribe().getName().contains('__c'))
options.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getName()));
}
return options;
}
}




2. Retrieveing all the field api names dynamically.The below samples helps to retrieve the field api names dynamically from the salesforce object.
page:
=====

 
<apex:page standardcontroller="Opportunity" extensions="DynamicVFClass">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1">
<apex:repeat value="{!fieldapis}" var="f">
<apex:inputField value="{!Opportunity[f]}"/>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


class:
======
public with sharing class DynamicVFClass {
public DynamicVFClass(ApexPages.StandardController controller) {
}
public List<String> getFieldapis() {
map<String, Schema.SObjectType> m1 = Schema.getGlobalDescribe() ;
SObjectType objToken1 = m1.get('Opportunity');
DescribeSObjectResult objDef1= objToken1.getDescribe();
map<String, SObjectField> fieldmap = objDef1.fields.getmap();
List<String> lstobjectfields1= new List<String>();
List<String> fieldLabels1= new List<String>();
map<String,String> fieldLabelNamemap1= new map<String,String>();
for (String fName1:fieldmap.keySet())
{
if(fieldmap.get(fName1).getDescribe().getType()!=Schema.DisplayType.Time &&
fieldmap.get(fName1).getDescribe().getType()!=Schema.DisplayType.anytype&&
fieldmap.get(fName1).getDescribe().getType()!=Schema.DisplayType.base64 &&
fieldmap.get(fName1).getDescribe().getType()!=Schema.DisplayType.EncryptedString &&
fieldmap.get(fName1).getDescribe().getType()!=Schema.DisplayType.Id &&
fieldmap.get(fName1).getDescribe().getType()!=Schema.DisplayType.multiPicklist &&

fieldmap.get(fName1).getDescribe().getType()!=Schema.DisplayType.TextArea)
{
fieldLabels1.add(fieldmap.get(fName1).getDescribe().getLabel());
fieldLabelNamemap1.put(fieldmap.get(fName1).getDescribe().getLabel(), fName1);
}
}
for (String fLabel1:fieldLabels1){
if(flabel1 !='Created Date'&& flabel1!='Last Activity Date' && flabel1!='Last modified Date' && flabel1!='Deleted' && flabel1!='System modstamp'&& flabel1!='')
{
//lstobjectfields.add(new selectOption(fieldLabelNamemap.get(flabel),fLabel));
lstobjectfields1.add(fieldLabelNamemap1.get(flabel1));
}
}
system.debug('#### All Fields are ####'+lstobjectfields1);
return lstobjectfields1;
}
}

Check this blog,
http://sfdcsrini.blogspot.com/2014/11/dynamic-apex-in-salesforce.html

 
Swarvi KumariSwarvi Kumari
Hi krishna,


    Once try this code.

    Visualforce page :
<apex:page controller="DynamicTableController123">
    <apex:form >
    
            <apex:actionFunction name="ObjectFileds" action="{!ObjectFields}"/>
<APEX:PAgeBlock >
     <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Select Object"/>
                <apex:selectList multiselect="false" size="1" value="{!SelectedObject}" onchange="ObjectFileds();">
                    <apex:selectOption itemLabel="--None--" itemValue="--None--"/>
                    <apex:selectoptions value="{!supportedObject}" />
                </apex:selectlist>
            </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Select Field"/>
                <apex:selectList multiselect="true" size="5" value="{!SelectedFields}">
                    <apex:selectOption itemLabel="--None--" itemValue="--None--"/>
                    <apex:selectoptions value="{!fieldLableAPI}" />
                </apex:selectlist>
            </apex:pageBlockSectionItem>
    
    
    
    </apex:pageblocksection>
    
    
    </APEX:PAgeBlock>
    
    
        </apex:form>
        </apex:page>

Controller :
public class DynamicTableController123 {

    public String SelectedObject {get; set;}
    public List<SelectOption> fieldLableAPI {get; set;}
    public List<selectoption> supportedObject {get; set;}
    public List<String> SelectedFields {get; set;}
    public List<sObject> ObjectList {get; set;}

 public DynamicTableController123 ()
    {
        //Initialize
        supportedObject = new List<selectoption>() ;
        SelectedObject = '' ;
        fieldLableAPI = new List<SelectOption>() ;
        SelectedFields = new List<String>() ;
        ObjectList = new List<sObject>() ;
        
        //Get only reference to objects
        for(Schema.SObjectType item : ProcessInstance.TargetObjectId.getDescribe().getReferenceTo())
        {
            //Excluding custom setting objects
            if(!item.getDescribe().CustomSetting)
            {
                //Adding to list
                supportedObject.add(new SelectOption(item.getDescribe().getLocalName().toLowerCase() , item.getDescribe().getLabel() ));
            }
        }
        
    }
    


 Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
    Set<String> objectKeys = gd.keySet();
 public void ObjectFields()
    {
        if(SelectedObject != '--None--')
        {
            //Creating sObject for dynamic selected object
            Schema.SObjectType systemObjectType = gd.get(SelectedObject);
            //Fetching field results
            Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
                
            Map<String, Schema.SObjectField> M = r.fields.getMap();
            //Creating picklist of fields
            for(Schema.SObjectField fieldAPI : M.values())
            {
                fieldLableAPI.add(new SelectOption(fieldAPI.getDescribe().getName() , fieldAPI.getDescribe().getLabel())) ;
            }
        }
    }

}

 
This was selected as the best answer
krishna guptakrishna gupta
Hi Swetha, can we do multiselect false for the "Select field"? When i was trying to false the value, it is not showing me any fields for any object.
krishna guptakrishna gupta
Hi Swetha, i resolved the problem. Your code is very helpful for me. Thanks!
Syed Subhan 16Syed Subhan 16
Anybody have same functionality code in lightning????????????????????????????? I am new to lightning.