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
Lalit Dhupar 13Lalit Dhupar 13 

get apex class id in vf page from dropdown selectiion

Hi All,

I have a requirement in which i need to display the names of all the apex classes in VF page drop down list and on selection of the apex class name, it should retrieve the id of the apex class. Could you please help.

Thanks!
Raj VakatiRaj Vakati
Use this code

 
public class dynamicpickone{
    
    public   String recordId{get;set;}
    public   ApexClass record{get;set;}
    public dynamicpickone() {
        record = new ApexClass();
    }
    public Id getApexClass() {
        return record.Id;
    }
    
    public void setApexClass(Id productId) {
        record.Id = productId;
    }
    public List<SelectOption> getApexClassList() {
        List<SelectOption> products = new List<SelectOption>();
        products.add(new SelectOption('', '--None--'));
        
        for (ApexClass p: [Select id ,Name,Body from ApexClass]) {
            products.add(new SelectOption(p.Id, p.Name));
        }
        
        return products;
    }
    public PageReference getUpdateCls(){
        System.debug('recordIdasaaa'+recordId);
        System.debug('record'+record);
        record =[Select id ,name,body from ApexClass where Id =:record.Id] ;
        return null ;
    }
    
    
}
 
<apex:page controller="dynamicpickone" showHeader="false" readOnly="true">
    <apex:form id="form">
        <apex:pageBlock title="Class View ">
            
            <apex:pageBlockSection title="Class Information">
                
                <apex:pageBlockSectionItem>
                    <apex:outputLabel for="productList" value="Apex Class" />
                    <apex:actionRegion>
                        
                        <apex:selectList value="{!ApexClass}" title="Product" size="1" id="products">
                            <apex:selectOptions value="{!ApexClassList}" />
                            <apex:actionSupport event="onchange" rerender="versions" action="{!getUpdateCls}">
                                <apex:param name="recordId" assignto="{!recordId}" value="{!ApexClass}"> </apex:param> 
                            </apex:actionSupport>
                            
                        </apex:selectList>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
                <apex:outputPanel id="versions"> 
                    
                    
                    <apex:pageBlock>
                        <apex:pageBlockSection>
                            {!record.Body}
                            
                        </apex:pageBlockSection>
                    </apex:pageBlock>
                    
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

 
Steven NsubugaSteven Nsubuga
Controller
public class AllApexClassesController {

    public String selectedClass { get; set; }
    
    public List<SelectOption> getAllApexClasses() {
        List<SelectOption> apexClasses = new List<SelectOption> { new SelectOption('','-- none --') };
        
        List<ApexClass> ApexClassList = [Select Id, Name from ApexClass];
        for(ApexClass aClass: ApexClassList ) {
            apexClasses.add(new SelectOption(aClass.Id, aClass.Name));
        }
        return apexClasses;
    }
    
    public PageReference showClassId() {
        return null;
    }
    
}


Visualforce Page
<apex:page controller="AllApexClassesController" lightningStylesheets="true" showHeader="false" sidebar="false">
    <apex:slds />
    <apex:form id="form">
        <apex:selectList value="{!selectedClass}" size="1">
            <apex:selectOptions value="{!AllApexClasses}" />
        </apex:selectList>
        <!--apex:commandButton value="Show Selected Class Id" action="{!showClassId}" rerender="out" status="status"/-->
    </apex:form>
    
    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel >
                    <p>You have selected: {!selectedClass}</p>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
</apex:page>

 
Lalit Dhupar 13Lalit Dhupar 13
Hi Steven,

You  have shown apex class body on vf page. Is it possible to input the selected apex class body and generate its test class automatically via code?
 
Steven NsubugaSteven Nsubuga
Hi Lalit, it is Raj who showed the class body on the vf page. 
About your question, I think this https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000EFozgUADo (https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000EFozgUAD) can help you do that.
Lalit Dhupar 13Lalit Dhupar 13
Hi Steven/Raj,

Is it possible without using salesforce package?
Steven NsubugaSteven Nsubuga
It is possible with a lot of effort. Maybe Raj can help.
Akshay_DhimanAkshay_Dhiman
Hi Lalit Dhupar,

Try for this code.
====================================       VF page       ============================================
<apex:page controller="dynamicpickone" showHeader="false" readOnly="true">
    <apex:form id="form">
        <apex:pageBlock title="Class View ">
            <apex:pageBlockSection title="Class Information">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel for="productList" value="Apex Class" />
                    <apex:actionRegion>
                        <apex:selectList value="{!ApexClass}" title="Product" size="1" id="products">
                            <apex:selectOptions value="{!ApexClassList}" />
                            <apex:actionSupport event="onchange" rerender="versions" action="{!getUpdateCls}">
                                <apex:param name="recordId" assignto="{!recordId}" value="{!ApexClass}"> </apex:param> 
                            </apex:actionSupport>
                        </apex:selectList>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
                <apex:outputPanel id="versions"> 
                    <apex:pageBlock>
                        <apex:pageBlockSection>
                            <div style="color:red">
                            ID = {!record.ID}  
                            </div>
                        </apex:pageBlockSection>
                        <apex:pageBlockSection>
                            {!record.Body}
                        </apex:pageBlockSection>
                      </apex:pageBlock>
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>  
</apex:page>

====================================       Controller       ============================================

public class dynamicpickone{
    
    public   String recordId{get;set;}
    public   ApexClass record{get;set;}
    
    public dynamicpickone() 
    {
        record = new ApexClass();
    }
    
    public Id getApexClass() 
    {
        return record.Id;
    }
    
      public void setApexClass(Id productId)
      {
        record.Id = productId;
      }
    
    public List<SelectOption> getApexClassList()
    {
        List<SelectOption> products = new List<SelectOption>();
        products.add(new SelectOption('','--None--'));
        
        for (ApexClass p: [Select id ,Name,Body from ApexClass]) 
        {
            products.add(new SelectOption(p.Id, p.Name));
        }
        return products;
    }
    public PageReference getUpdateCls()
    {
         record =[Select id ,name,body from ApexClass where Id =:record.Id] ;
         return null ;
    }    
}

Please mark it as solved if my reply is helpful for you and it clears your all doubts about salesforce sites. 

Thank You
Akshay