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
AyanHoreAyanHore 

List and fetch all metadata types programmatically

Hi,

I'm trying to list and display all metadata components in a VF page. It should ideally have two dropdowns: one for metadata types (apex class, visualforce page, custom settings, apex component, etc) and another (dependent on the first one) will list all the available ones in the org. Is there any way to list them programmatically using Apex code?  It will be something similiar to "Metadata Types & Components" in the Workbench Tool.

I understand that need to use dynamic apex, however any help/directions will be very helpful to me.

~Ayan
sandeep sankhlasandeep sankhla
Hi Ayan,

You can make use of meta data api here in apex.
Sampath KumarSampath Kumar
Hi Ayan,

Please find the visualforce page and apex class to retrive meta data of visualforce page, custom components and apex classes.

Visualforce page
===========
<apex:page controller="RrtriveObjects">
<apex:form > 
      <apex:pageBlock >
          <apex:pageBlockSection columns="1">

              <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:dataTable value="{!ObjectFields}" var="a">
                       <apex:column value="{!a.Id}"/>     
                          <apex:column value="{!a.name}"/>
                          
                          
                          </apex:dataTable>
                        </apex:actionRegion>      
                     </apex:outputPanel>
              </apex:pageBlockSectionItem> 

          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

Apex Class
========
public class RrtriveObjects
{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    public String selectedObject {get; set;}

    public String selectedField {get; set;}

    Public RrtriveObjects()
    {   
        selectedObject = 'Classes';
    }

    public List<SelectOption> getObjectNames() 
    {
        List<SelectOption> objNames = new List<SelectOption>();
       
            objNames.add(new SelectOption('Classes','Classes'));
            objNames.add(new SelectOption('Pages','pages'));
            objNames.add(new SelectOption('Components','Components'));
        return objNames;
     }

     public List<ApexClass> getObjectFields() 
     { 
            
            List<sobject> ac = new List<sobject>();
            if(selectedObject == 'Classes'){
            ac =[select Id,name from ApexClass];
            
            }
            else if(selectedObject == 'Pages')
            {
            ac =[select Id,name from ApexPage];
            }
            else{
            ac =[select Id,name from ApexComponent];
            }
            return ac;
      }      
}

Please let me know if this solves your query.

Regards
Sampath Kumar Goud