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
QLearnQLearn 

Dynamically objects should appear in picklist field of org

Hi all,

Please help me in resolving this

 

Dynamically objects of org should populate in picklist field, when i select an object , its API name, object name, singular name, fields available in that object should appear in pageblock table below.

 

I would really appreciate if someone help me in solving this.

 

Thanks in advance.

 

hope i made the question clear.

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

...........

Page

..........

<apex:page Controller="New1">  

   <apex:form id="Describe">

      <apex:pageBlock id="block2" >

         <apex:pageblockbuttons location="top" >

            <apex:commandButton value="Get Describe Object Fields" action="{!showFields}"/>

         </apex:pageblockbuttons>

        <apex:pageblocksection >

          <apex:selectList value="{!selectedObject}" size="1">

           <apex:selectOptions value="{!objectNames}"/>

          </apex:selectList>

         

        </apex:pageblocksection>

        <apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">

            <apex:panelBar items="{!fields}" var="fls">

            <apex:actionSupport event="onclick" action="{!GetSelected}"  />

             <apex:panelBarItem label="{!fls.key}">{!fls.val}</apex:panelBarItem>

            </apex:panelBar>

      

        

          </apex:pageBlockSection>

       </apex:pageBlock>

    </apex:form>

</apex:page>

 

 

............

Controller

............

 

public class New1

{

 

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

    public List<Pair> lstfieldname{get;set;}

    public List <Pair> fields {get{return lstfieldname;} set{lstfieldname =value;}}

    public List <SelectOption> objectNames{public get; private set;}

    public String selectedObject {get; set;}

    public String show {get; set;}

 

  public void GetSelected() {

  show='Sourabh';

      

    }

    // Intialize objectNames and fields

    

    public New1() {

        objectNames = initObjNames();

        fields = new List<Pair>();

    }

    // Populate SelectOption list  - 

    

    // find all sObjects available in the organization 

    

    private List<SelectOption> initObjNames() {

        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() {

        fields.clear();

        system.debug('$$$$$' + selectedObject);

        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();

        for(Schema.SObjectField sfield : fieldMap.Values())

        {

        schema.describefieldresult dfield = sfield.getDescribe();

        system.debug('#######'  + dfield );   

        Pair field = new Pair();    

        field.key = dfield.getname();

         

        system.debug('#######4444'  + field.key);

        field.val = dfield.getType () + ' : ' + dfield.getLabel ();   

        lstfieldname.add(field);

        show=field.key;

        }

        }

       

  public class Pair

  {

     public String key {get; set;}

     public String val {get; set;}     

  }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

...........

Page

..........

<apex:page Controller="New1">  

   <apex:form id="Describe">

      <apex:pageBlock id="block2" >

         <apex:pageblockbuttons location="top" >

            <apex:commandButton value="Get Describe Object Fields" action="{!showFields}"/>

         </apex:pageblockbuttons>

        <apex:pageblocksection >

          <apex:selectList value="{!selectedObject}" size="1">

           <apex:selectOptions value="{!objectNames}"/>

          </apex:selectList>

         

        </apex:pageblocksection>

        <apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">

            <apex:panelBar items="{!fields}" var="fls">

            <apex:actionSupport event="onclick" action="{!GetSelected}"  />

             <apex:panelBarItem label="{!fls.key}">{!fls.val}</apex:panelBarItem>

            </apex:panelBar>

      

        

          </apex:pageBlockSection>

       </apex:pageBlock>

    </apex:form>

</apex:page>

 

 

............

Controller

............

 

public class New1

{

 

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

    public List<Pair> lstfieldname{get;set;}

    public List <Pair> fields {get{return lstfieldname;} set{lstfieldname =value;}}

    public List <SelectOption> objectNames{public get; private set;}

    public String selectedObject {get; set;}

    public String show {get; set;}

 

  public void GetSelected() {

  show='Sourabh';

      

    }

    // Intialize objectNames and fields

    

    public New1() {

        objectNames = initObjNames();

        fields = new List<Pair>();

    }

    // Populate SelectOption list  - 

    

    // find all sObjects available in the organization 

    

    private List<SelectOption> initObjNames() {

        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() {

        fields.clear();

        system.debug('$$$$$' + selectedObject);

        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();

        for(Schema.SObjectField sfield : fieldMap.Values())

        {

        schema.describefieldresult dfield = sfield.getDescribe();

        system.debug('#######'  + dfield );   

        Pair field = new Pair();    

        field.key = dfield.getname();

         

        system.debug('#######4444'  + field.key);

        field.val = dfield.getType () + ' : ' + dfield.getLabel ();   

        lstfieldname.add(field);

        show=field.key;

        }

        }

       

  public class Pair

  {

     public String key {get; set;}

     public String val {get; set;}     

  }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
QLearnQLearn

Thankz for the solution. I really appreciate your work.

 

I have some additional requirment to the same., need to export the object fields into excel sheet. 

 

This ends with my Task..

 

 

 

 

 

 

 

QLearnQLearn

<apex:page controller="ObjectFetch5_CLS" sidebar="false" >
<apex:form >
    <apex:pageBlock id="block2" >
        <apex:outputLabel ><b>Objects :</b></apex:outputLabel>&nbsp;
        <apex:selectList value="{!selectedObject}" size="1">
            <apex:actionSupport event="onchange"  reRender="detail"/>
            <apex:selectOption itemLabel="--Select Object--" itemValue="blank" />
            <apex:selectOptions value="{!objectNames}"/>
        </apex:selectList>&nbsp;&nbsp;&nbsp;

        <apex:commandButton value="Get Fields" action="{!showFields}" reRender="block2" status="fetchStatus" />&nbsp;
               
        <apex:actionStatus id="fetchStatus" >
            <apex:facet name="start">
                <img src="/img/loading.gif" class="waitingImage" title="Please Wait..."/>
            </apex:facet>
        </apex:actionStatus>    <br/><br/>
       
        <apex:pageBlockTable value="{!InrField}" var="fls" rendered="{!showTable}" >
            <apex:column headerValue="Field Value" value="{!fls.val}" />
            <apex:column headerValue="Field Name" value="{!fls.key}" />
            <apex:column headerValue="Field Type" value="{!fls.type}" />               
        </apex:pageBlockTable>    <br/><br/>
       
        <apex:commandButton value="Export" action="{!exportData}" rendered="{!showButton}" />
               
      <!--  <apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">
            <apex:panelBar items="{!InrField}" var="fls">
                <apex:panelBarItem >{!fls.val}</apex:panelBarItem>
                <apex:panelBarItem label="{!fls.key}">{!fls.val}</apex:panelBarItem>               
            </apex:panelBar>  
        </apex:pageblocksection>    -->
    </apex:pageBlock>
   

</apex:form>
</apex:page>

====================================================

public class ObjectFetch5_CLS {
    public String selectedObject {get; set;}
    public List <InrPair> InrField {get; set;}
    public List <SelectOption> objectNames {get; set;}
    public Boolean showTable {get; set;}
    public Boolean showButton {get; set;}
   
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
      
    public ObjectFetch5_CLS() {
        objectNames = initObjNames();
        InrField = new List<InrPair>();
        showTable = false;
        showButton = false;
    }

    private List<SelectOption> initObjNames() {
        List<SelectOption> objNames = new List<SelectOption>();
     //   objNames.add(new SelectOption('blank', '--Select Object--'));
        List<Schema.SobjectType> obj = Schema.getGlobalDescribe().Values();
        for(Schema.SobjectType ss:obj) {
            objNames.add(new SelectOption(ss.getDescribe().getName(), ss.getDescribe().getName()));
            objNames.sort();
        }
        return objNames;
    }


/*    private List<SelectOption> initObjNames() {
        List<SelectOption> objNames = new List<SelectOption>();
        objNames.add(new SelectOption('blank', '--Select Object--'));
        List<String> str = new List<String>(schemaMap.keySet());
        str.sort();
        for(String ss : str) {
            objNames.add(new SelectOption(ss,ss));
        }
        return objNames;
    }    */

 

// Find the InrField for the selected object

    public void showFields() {
     //   InrField.clear();
        if(selectedObject != 'blank') {
            Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
            for(Schema.SObjectField sfield : fieldMap.Values()) {
                schema.DescribeFieldResult dfield = sfield.getDescribe();
                InrPair fld = new InrPair();
                fld.key = dfield.getName();
                fld.val = dfield.getLabel();
                fld.type = string.valueof(dfield.getType());
                InrField.add(fld);
            }
            showTable = true;
            showButton = true;
         }
         else {
            showTable = false;
            showButton = false;
         } 
    }
   
   
    public PageReference exportData() {
        PageReference pf = new PageReference('/apex/ObjectFetch6');
        pf.getParameters().put('objVal', selectedObject);
        pf.setRedirect(true);
        return pf;   
    }   

    public class InrPair {
        public String key {get; set;}
        public String val {get; set;}
        public string type {get; set;}
    }
}

 

==================================================================================================

------------------------------------------------------------------------------------------------------------------------------------------------------------

==================================================================================================

 

<apex:page controller="ObjectFectch6_CLS" contentType="application/vnd.ms-excel#FILENAME.xls" cache="true"  sidebar="false" >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!InrField}" var="fls"  >
            <apex:column headerValue="Field Value" value="{!fls.val}" />
            <apex:column headerValue="Field Name" value="{!fls.key}" />
            <apex:column headerValue="Field Type" value="{!fls.type}" />               
        </apex:pageBlockTable>    
    </apex:pageBlock>
</apex:page>

========================================================================

public class ObjectFectch6_CLS {
    public List<InrPair> InrField {get; set;}
    public string str;
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
   
    public ObjectFectch6_CLS() {
        str = ApexPages.CurrentPage().getParameters().get('objVal');
        try{
            InrField = new List<InrPair>();
            if(str != 'blank') {
                Map <String, Schema.SObjectField> fieldMap = schemaMap.get(str).getDescribe().fields.getMap();
                for(Schema.SObjectField sfield : fieldMap.Values()) {
                    schema.DescribeFieldResult dfield = sfield.getDescribe();
                    InrPair fld = new InrPair();
                    fld.key = dfield.getName();
                    fld.val = dfield.getLabel();
                    fld.type = string.valueof(dfield.getType());
                    InrField.add(fld);
                }       
            }
         }catch(Exception e) {}  
    }
   
    public class InrPair {
        public String key {get; set;}
        public String val {get; set;}
        public string type {get; set;}
    }

}