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
hari azmeera 8hari azmeera 8 

query all sobjects and store them in a picklist

Pankaj_GanwaniPankaj_Ganwani
Hi Hari,

You can use describe calls for this:

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
List<SelectOption> lstSO = new List<SelectOption>();

for(String strName : gd.keyset())
{
   lstSO.add(new SelectOption(strName,strName));
}

Bind this selectoption list with page.

<apex:selectList value="{!val}">
   <apex:selectOptions value="{lstSO}"/>
</apex:selectList>
JyothsnaJyothsna (Salesforce Developers) 
Hi Hari,

Please check the below sample code

VisualForce Page:
<apex:page controller="QueryAllFieldscontrol">
  <apex:form >
  <apex:pageBlock >
  <apex:pageBlockButtons location="bottom">
  <apex:commandButton value="Fetch" action="{!search}"/>
  </apex:pageBlockButtons>
  <apex:pageBlockSection >
  <apex:selectList value="{!listval}" size="1" label="Sobjects">
  <apex:selectOptions value="{!sobjectnames}">
  </apex:selectOptions>
  <apex:actionSupport action="{!Fetch}" event="onchange"/>
  </apex:selectList>
  </apex:pageBlockSection>
  <apex:pageBlockSection title="Query">
  <apex:inputTextarea value="{!fields1}" rows="3" cols="100" label="Query of {!listval} object:" />
  </apex:pageBlockSection>
  <apex:pageBlockTable value="{!allsobj}" var="obj1">
 <apex:repeat value="{!qfiled}" var="qf">
 <apex:column value="{!obj1[qf]}"/>
 </apex:repeat>
  
  </apex:pageBlockTable>
  </apex:pageBlock>
  </apex:form>
</apex:page>

Controller:
 
public with sharing class QueryAllFieldscontrol {

    public list<String> qfiled { get; set; }

    public list<sobject> allsobj { get; set; }

    public String fields1 { get; set; }

  public QueryAllFieldscontrol (){
  allsobj=new list<sobject>();
  qfiled =new list<string>();
  
  }
    public PageReference Fetch() {
    //str=null;
    allsobj.clear();
    qfiled.clear(); 
     map<string,Schema.SObjectType> fd = Schema.getGlobalDescribe();
    map<String,Schema.SobjectField> fc=fd.get(listval).getDescribe().fields.getMap();
    set<string> se=fc.keyset();
    str='select';
        for(string sc:se)
        {
          str=sc+'';  
          qfiled.add(sc);
                 
          }
          str+='from'+listval;
          
    
        return null;
    }


    public list<selectoption> getSobjectnames() {
    list<schema.sobjecttype> gd= schema.getglobaldescribe().values();
    list<selectoption> options =new list<selectoption>();
    for(schema.sobjecttype gh:gd)
    {
       options.add(new selectoption(gh.getdescribe().getname(),gh.getdescribe().getname()));
    }
        return options;
    }

  public string str;
    public String listval { get; set; }
    list<sobject> query1=new list<sobject>();

   public PageReference search() {
   String SobjectApiName = listval;
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
 
        String commaSepratedFields = '';
        for(String fieldName : fieldMap.keyset()){
            if(commaSepratedFields == null || commaSepratedFields == ''){
                commaSepratedFields = fieldName;
            }else{
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            }
        }
 
        fields1=  'select '+commaSepratedFields+' from ' + SobjectApiName ;
   
     
    
     allsobj =database.query(fields1);
     //system.debug(query1);
      return  null;
     
    }

}

Please refer the below link for more details.
http://sfdcsrini.blogspot.com/2014/11/dynamic-apex-in-salesforce.html
Hope this helps you!
Best Regards,
Jyothsna
Amit Chaudhary 8Amit Chaudhary 8
Try below code
<apex:page controller="DescibeDemoController">
    <apex:form id="Describe">
        <apex:pageBlock id="block2" >
            <apex:pageblockbuttons location="top" >
                    <apex:commandButton value="Show Fields" action="{!showFields}" />
            </apex:pageblockbuttons>
            
            <apex:pageblocksection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Object Name</apex:outputLabel>
                    <apex:selectList value="{!selectedObject}" size="1">
                        <apex:selectOptions value="{!ListObejectName}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageblocksection>
        </apex:pageBlock>
        
        <apex:pageBlock id="result" title="Field Detail for {!selectedObject}" rendered="{!if(listField.size > 0 ,true,false)}"   >
            <apex:pageBlockTable value="{!listField}" var="field" rendered="{!if(listField.size > 0 ,true,false)}"> 
                <apex:column value="{!field.fieldName }" headerValue="Name" />
                <apex:column value="{!field.fieldAPIName }"  headerValue="API Name"/>
            </apex:pageblockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>
public with sharing class DescibeDemoController 
{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public String selectedObject {get; set;}
    public List<FieldWrapper> listField{get;set;}

    public DescibeDemoController() 
    {
        listField = new List<FieldWrapper>();
    }

    // find all sObjects available in the organization
    public  List<SelectOption> getListObejectName() 
    {
        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;
    }

    
    // Find the fields for the selected object
    public void showFields() 
    {
        listField.clear();
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.Values())
        {
            schema.describefieldresult dfield = sfield.getDescribe();
            FieldWrapper wObj = new FieldWrapper();
            wObj.fieldName = dfield.getLabel ();
            wObj.fieldAPIName = dfield.getname();
            listField.add(wObj);
        }
    }

    public class FieldWrapper
    {
        public String fieldName {get; set;}
        public String fieldAPIName {get; set;}
    }

}
User-added image

Let us know if this will help you


 
Ravan557Ravan557

Hi ....hari azmeera 8

VF Page Cde


<apex:page controller="RecordPicklist">
  <apex:form >
     <apex:pageBlock >
       <apex:pageBlockSection >
         <apex:pageBlockSectionItem >
             <apex:outputLabel value="SObject List"/>
           <apex:selectList size="1">
             <apex:selectOptions value="{!objects}"/>
           </apex:selectList>
         </apex:pageBlockSectionItem>
       </apex:pageBlockSection>
     </apex:pageBlock> 
  </apex:form>
</apex:page>


Controller Code

public with sharing class RecordPicklist {

    public List<SelectOption> objects { get; set; }
    
    public List<String> objlst { get; set; }
    
    public Map<String, Schema.SobjectType> mobjects { get; set; } 
    
    public RecordPicklist(){
   
    objects = new List<SelectOption>();
  
    objlst = new List<String>();
   
    mobjects = Schema.getGlobalDescribe();
  
    objlst.addAll(mobjects.keyset());
   
    objlst.sort();

    selectoption ip = new selectoption('None','---None---');

    objects.add(ip);
   
       for(String s:objlst){
   
       selectoption op = new selectoption(s,s);
       
       objects.add(op);
   
       }
   } 
}

Enjoy.........