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
salesforce@14salesforce@14 

can anyone help me to display all the standard objects in one picklist and when i select any one object, then all the records in that objects have to display.

Best Answer chosen by salesforce@14
Chidambar ReddyChidambar Reddy
Hi Sushma, I have tested this,
 
<apex:page controller="ClassName">
    <apex:form >
        <apex:selectList value="{!choosenObject}" size="1">

              <apex:selectoptions value="{!objects}"/>

        </apex:selectList>


<br/>
       <apex:commandbutton value="Query" action="{!queryMe}" rerender="output"/>

         <apex:dataList id="output" value="{!records}" var="r">
         {!r}
        </apex:dataList>

    </apex:form>

</apex:page>
 
public class ClassName{
       
        public List<SelectOption> objects {get; set;}
        public List<SObject> records {get; set;}
        public string choosenObject {get; set;}
 
        public ClassName(){
               objects = new List<SelectOption>();
               records = new List<SObject>();
               Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
               List<String> objectList = new List<String>();
               objectList.addAll(gd.keyset());
               Schema.DescribeSobjectResult[] results = Schema.describeSObjects(objectList );
               for(Schema.DescribeSobjectResult res : results) { 
                    if( ! res.isCustom()) 
                          objects.add(new selectOption(res.getName() ,res.getLabel())); 
               }

        }

        public void queryMe(){
               if(string.isNotBlank(choosenObject))
               records = database.query('select Id, Name from '+choosenObject);
               //you may need to remove Name field for some objects
        }
}

 

All Answers

Chidambar ReddyChidambar Reddy
Hi Sushma,
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
List<SelectOption> standardObjects = new List<SelectOption>();
List<String> objectList = new List<String>();
objectList.addAll(gd.keyset());


Schema.DescribeSobjectResult[] results = Schema.describeSObjects(objectList ); 
for(Schema.DescribeSobjectResult res : results) {
if( ! res.isCustom()) 
standardObject.add(new selectOption(res.getName() ,res.
getLabel());

 }

use this select option to generate picklist in visual force page and use dynamic query to return results


Thank you
Choose it as best answer if it has helped you
MithunPMithunP
Hi Sushma,

You can use below code for displaying only standard objects in a picklist.
 
public with sharing class dynamic1cls {
    
     public list<selectoption> lst{get;set;}
     public String Obname { get; set; } 
       public dynamic1cls(){
          List<Schema.SobjectType> lstobj = Schema.getGlobalDescribe().Values();
          lst = new List<selectoption>();
          for(Schema.SobjectType s: lstObj){
             if(!string.valueOf(s).contains('__c')){
                 lst.add(new selectoption(string.valueOf(s),string.valueOf(s)));
             }
         }
        
    }

}
<apex:page controller="dynamic1cls" >
     <apex:form >
         <apex:selectList size="1" value="{!Obname}" style="width:200px">
               <apex:selectOptions value="{!lst}"> 
               </apex:selectOptions>
         </apex:selectList>
     </apex:form>
</apex:page>


Best Regards,
Mithun.
 
salesforce@14salesforce@14

Hi,
Thanks for your support.

I am getting till displaying all the records of all objects in one page, but i want to display like if i click on Account in picklist then particular object records should to be display and same for all other standard objects.
can you help me for this.

Thanks,
Sushma.
MithunPMithunP
Hi Sushma,

You can use below code
public class DynamicTableController  
    {  
        //List displayed on UI  
        public List<selectoption> supportedObject {get; set;}  
          
        //Selected Object  
        public String SelectedObject {get; set;}  
          
        //Global describe  
        Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();  
        //Set<String> objectKeys = gd.keySet();  
          
        //Field Select List  
        public List<SelectOption> fieldLableAPI {get; set;}  
          
        //Selected fields to be displayed in table  
        public List<String> SelectedFields {get; set;}  
          
        //List to maintain dynamic query result  
        public List<sObject> ObjectList {get; set;}  
          
          
        //Constructor  
        public DynamicTableController()  
        {  
            //Initialize  
            supportedObject = new List<selectoption>() ;  
            SelectedObject = '' ;  
            fieldLableAPI = new List<SelectOption>() ;  
            SelectedFields = new List<String>() ;  
            ObjectList = new List<sObject>() ;  
            //List<Schema.sObjecttype> lst = Schema.getGlobalDescribe().values();
            //Get only reference to objects  
     for(Schema.SObjectType item :gd.values())  
            {  
                //Excluding custom setting objects  
                if(!item.getDescribe().CustomSetting)  
             {  
                 //Adding to list  
                supportedObject.add(new SelectOption(item.getDescribe().getLocalName() , item.getDescribe().getLabel() ));  
              }  
            }  
              
        }  
          
        //Get fields of selected object  
        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.get
                describe().getLabel())) ;  
                }  
            }  
        }  
        public void ShowTable()  
        {  
            //Creating dynamic query with selected field  
            String myQuery = 'Select Id ' ;  
              
            for(String field : SelectedFields)  
            {  
                if(field.toLowerCase() != 'id' && field.toLowerCase() != '--none--')  
                myQuery += ','+ field + ' ' ;  
            }  
              
            //Limit is 100 for now you can change it according to need  
            myQuery += ' from ' + SelectedObject + ' LIMIT 100' ;  
              
            //Executing the query and fetching results  
            ObjectList = Database.query(myQuery) ;  
        }  
    }

Best Regards,
Mithun.
 
salesforce@14salesforce@14
Thanks.

My requirement is if i select object in picklist, then i have to display the records.
 
Chidambar ReddyChidambar Reddy
Hi Sushma,

The apex class as follows
 
public class ClassName{
       
        public List<SelectOption> objects {get; set;}
        public List<SObject> records {get; set;}
        public string choosenObject {get; set;}
 
        public ClassName(){
               objects = new List<SelectOption>();
               records = new List<SObject>();
               Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
               List<String> objectList = new List<String>(); objectList.addAll(gd.keyset());
               Schema.DescribeSobjectResult[] results = Schema.describeSObjects(objectList );
               for(Schema.DescribeSobjectResult res : results) { 
                    if( ! res.isCustom()) 
                          objects.add(new selectOption(res.getName() ,res. getLabel()); 
               }

        }

        public void queryMe(){
               if(string.isNotBlank(choosenObject)
               records = database.query('select Id, Name from '+choosenObject);
               //you may need to remove Name field for some objects
        }

​}

and the Vf page
 
<apex:page controller="ClassName">
    <apex:form>
        <apex:selectList value="{!choosenObject}">

              <apex:selectoptions value="{!objects}">

        </apex:selectList>


<br/>
       <apex:commandbutton value="Query" action="{!queryMe}" rerender="output">

         <apex:dataList id="output" value={!records} var="r">
         {!r}
        </apex:dataList>

    </apex:form>

</apex:page>
You need to accept that every object has different set of coulmns and data types, so you may need use links to go to corresponding object links

like for Account '/001 ' and contact '/003' etc



Thanks 
Choose it as best answer if it has helped you
 
salesforce@14salesforce@14
Hi Chidambar Reddy,

For me no need to go to list view page of any objects. I wants to get all the records from standard objects and to display it in one page and I did upto it.

I dont know how to display the records for particular selected objects in picklist.

Thanks for your support.
Chidambar ReddyChidambar Reddy
I have given the same in the above comment, but it will only dispaly record ids of the selected object and even if you query other fields you can't manage it as a table

Have you tried the above code?
Chidambar ReddyChidambar Reddy
Hi Sushma, I have tested this,
 
<apex:page controller="ClassName">
    <apex:form >
        <apex:selectList value="{!choosenObject}" size="1">

              <apex:selectoptions value="{!objects}"/>

        </apex:selectList>


<br/>
       <apex:commandbutton value="Query" action="{!queryMe}" rerender="output"/>

         <apex:dataList id="output" value="{!records}" var="r">
         {!r}
        </apex:dataList>

    </apex:form>

</apex:page>
 
public class ClassName{
       
        public List<SelectOption> objects {get; set;}
        public List<SObject> records {get; set;}
        public string choosenObject {get; set;}
 
        public ClassName(){
               objects = new List<SelectOption>();
               records = new List<SObject>();
               Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
               List<String> objectList = new List<String>();
               objectList.addAll(gd.keyset());
               Schema.DescribeSobjectResult[] results = Schema.describeSObjects(objectList );
               for(Schema.DescribeSobjectResult res : results) { 
                    if( ! res.isCustom()) 
                          objects.add(new selectOption(res.getName() ,res.getLabel())); 
               }

        }

        public void queryMe(){
               if(string.isNotBlank(choosenObject))
               records = database.query('select Id, Name from '+choosenObject);
               //you may need to remove Name field for some objects
        }
}

 
This was selected as the best answer
salesforce@14salesforce@14
Thanks for your support Chidambar Reddy.

Is there any other way to display the other fields also.
Chidambar ReddyChidambar Reddy
Yes, we can display other fields as well, I can't provide you the solution as it takes a lot of time, but I can provide you the way to solve it

for that you need to select which fields you want to query and use a wrapper to hold the state of selected fields
use dynamic table coulmns that will render based on fields selected

You can get fields of the selected fields by using schema classes 'ObjectName.SobjectType.getDescribe().getFields()' like this.


Thanks 
Choose it as best answer if you found my answer helpful
salesforce@14salesforce@14

Apex class:

public class SobjectList{
  public string val {get;set;}
  public string obj{get;set;}
 public String AccountList { get; set; }    
    //Subclass : Wrapper Class 
    public class Accountwrap {
        //Static Variables 
        public string id;
        public string name;
        public string Phone;     
        //Wrapper  Class Controller
        Accountwrap() {
            Phone = '';
        }       
    }      
    //Method to bring the list of Account and Serialize Wrapper Object as JSON
    public  static String getlstAccount() {
        List < Accountwrap > lstwrap = new List < Accountwrap > ();
        List < account > lstacc = [SELECT Id, Name, Phone
                                   FROM Account];                               
        for (Account a: lstacc) {
            Accountwrap awrap = new Accountwrap();
            awrap.id = a.id;
            awrap.name = a.name;
            if (a.Phone != null) {
                awrap.Phone = a.Phone;
            }
            lstwrap.add(awrap);
        }
        return JSON.serialize(lstwrap);
     }
}

can u do the modification of using schema class in it.

Thanks.