Skip to main content
Here it is my code:-

==========controller===========

public class Dynamic_Table {

    

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

    public String selectedObject {get; set;}

    public String selectedField {get; set;}

   

    Public Dynamic_Table()

    {   

        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;

      }   

  

}

===========vf page==============

<apex:page controller="Dynamic_Table" lightningStylesheets="true">

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

 
4 respostas