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
SOUBHAGYA RANJAN DALAISOUBHAGYA RANJAN DALAI 

retriving all the object in vf page

i want to retrive all the objects in my vf page as dropdownlist . and after selecting one object i want to diplsy all the mandetory field of that object with its datatype . 
Mudasir WaniMudasir Wani
Hello Soubhagya,

This is a similar kind of requirement and Will help you.



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;
}
}



Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
 
Sampath KumarSampath Kumar

Hello Soubhagya,

Please find the visualforce page and apex class below which solves your requirement.

Visualforce page:
------------------------

<apex:page controller="objectController1"> <apex:form > <apex:pageBlock > <apex:pageBlockSection columns="2"> <apex:pageBlockSectionItem > <apex:outputlabel value="Object Names :"/> <apex:actionRegion > <apex:selectList value="{!selectedObject}" size="1"> <apex:selectOptions value="{!ObjectNames}"/> <apex:actionSupport event="onchange" rerender="myFields"/> </apex:selectList> </apex:actionRegion> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputlabel value="Field Names :"/> <apex:outputPanel id="myFields"> <apex:actionRegion > <apex:selectList value="{!selectedField}" size="1"> <apex:selectOptions value="{!ObjectFields}"/> </apex:selectList> </apex:actionRegion> </apex:outputPanel> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
 

Class
--------
public class objectController1
{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    public String selectedObject {get; set;}

    public String selectedField {get; set;}

    Public objectController1()
    {   
        selectedObject = 'account';
    }

    public List<SelectOption> getObjectNames() 
    {
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
        {
            objNames.add(new SelectOption(name,name));
        }
        return objNames;
     }

     public List<SelectOption> getObjectFields() 
     {
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
            Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
            List<SelectOption> fieldNames = new List<SelectOption>();
            for (String fieldName: fieldMap.keySet()) 
            {  
                fieldNames.add(new SelectOption(fieldName,fieldName));
              //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
            }
            return fieldNames;
      }       
}

Mark this as solved if this answers your query.

Regards
Sampath Kumar Goud

SOUBHAGYA RANJAN DALAISOUBHAGYA RANJAN DALAI

Hi Sampath 

i tried this but showing error on controller . 
 
Public objectController1()
    {   
        selectedObject = 'account';
    }

showing error on this .. can you pls find out the solution .
ERROR = The property String objnames is referenced by Visualforce Page (object) in salesforce.com. Remove the usage and try again.
thanks
soubhgya

Sampath KumarSampath Kumar
Hi Soubhagya,

I think you have created a page with name ObjectRetrieve which is suggested by Mudasir Wani, delete the page and try again. Hope this will solve your issue. 

Regards
Sampath Kumar Goud