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
virtuous invinciblesvirtuous invincibles 

how can i get list of records by selecting an object from a select list

NagendraNagendra (Salesforce Developers) 
Hi Invincibles,

 I have solution for your problem, which is  using salesforce schema and describe methods.
  See how nice salesforce is handling all these metadata information!!!.
 
  Below is the attched Visualforce Markup and Apex code.
<apex:page controller="objectController">
    <apex:form>
         <apex:selectList  value="{!object_Name}" size="1">
          <apex:selectOptions  value="{!options_Objects}"/>
              <apex:actionSupport  action="{!childList}"  event="onchange"/>
         </apex:selectList>
         <apex:selectList   >
              <apex:selectOptions  value="{!options_Childs}"/>
         </apex:selectList>
    </apex:form> 
</apex:page>
public class objectController
{
 
  Public  List<SelectOption> options_Objects {get;set;}
  Public  List<SelectOption> options_Childs  {get;set;}
  Public  String object_Name{get;set;} 
  
  public  objectController()
  {
       options_Objects  =   new  List<SelectOption>();
       options_Childs   =   new  List<SelectOption>();
       List<Schema.SObjectType> GlobalDescribe     = Schema.getGlobalDescribe().Values();
       for( Schema.SObjectType ObjectType :GlobalDescribe)
       {
        options_Objects.add( new SelectOption( ObjectType.getDescribe().getLocalName() ,  ObjectType.getDescribe().getLabel() ) );  
       } 
   } 
   public void childList()
   { 
        options_Childs.clear();
        Schema.DescribeSObjectResult R =  Schema.getGlobalDescribe().get( object_Name).getDescribe();
        for( Schema.ChildRelationship  Child: R.getChildRelationships())
        {
         options_Childs.add( new SelectOption( Child.getChildSObject().getDescribe().getLocalName() ,  Child.getChildSObject().getDescribe().getLabel()  ) );  
        }
     
   }




}
Hope this helps.

Kindly mark this as solved if the reply was helpful so that it gets removed from the unanswered queue.

Thanks,
Nagendra


 
virtuous invinciblesvirtuous invincibles
Thanks for your reply nagendra but i dont need child ralated feilds i need records list
for example if i select the account object it should display all the account records

i appreciate if you could help me
Ajay K DubediAjay K Dubedi
Hi Virtuous,

please refer the below code, hope it will help you.

//VF Page:-
<apex:page controller="objectController">
<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>

//Controller:-

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

    public String selectedObject {get; set;}

    public String selectedField {get; set;}

    Public objectController()
    {   
        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;
      }       
}

please mark it as the best answer if it help you.

Thank You,
Ajay Dubedi