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
Sarita Swain 9Sarita Swain 9 

can anyone help me to display all the standard objects in one picklist and when i select any one object, then all those fields which are queryable will display as a multi select picklist.

Ravi Dutt SharmaRavi Dutt Sharma
Hi Sarita,

You can use the Schema class to achieve this.

getGlobalDescribe()
Returns a map of all sObject names (keys) to sObject tokens (values) for the standard and custom objects defined in 


 
Vasani ParthVasani Parth
Sarita, here you go:

Visualforce Page
<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>


Apex Class
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
        }
}

Please mark this as the best answer if this helps
Sarita Swain 9Sarita Swain 9
Thanks Ravi..
 
Sarita Swain 9Sarita Swain 9
Thanks vasani...