+ Start a Discussion
adi salesforceadi salesforce 

Code to create Drop down list for all objects.

Best Answer chosen by adi salesforce
bhanu_prakashbhanu_prakash
Hi Adi,
Mark as best answer, If it resloves !!​​
Controller
public class DisplayListSobjectsController {
    public String selectedObject { get; set; }
    public String selectedField { get; set; }
    public SelectOption[] getObjectOptions() {
        SelectOption[] results = new SelectOption[] { new SelectOption('','-- none --') };
        for(SObjectType sType: Schema.getGlobalDescribe().values()) {
            DescribeSObjectResult res = sType.getDescribe();
            results.add(new SelectOption(res.getName(), res.getLabel()));
        }
        return results;
    }
    public SelectOption[] getFieldOptions() {
        SelectOption[] results = new SelectOption[] { new SelectOption('','-- none --') };
        if(selectedObject != null) {
            for(SObjectField sField: Schema.getGlobalDescribe().get(selectedObject).getDescribe().fields.getMap().values()) {
                DescribeFieldResult res = sField.getDescribe();
                results.add(new SelectOption(res.getName(), res.getLabel()));
            }
        }
        return results;
    }
}
Visualforce Page
<apex:page controller="DisplayListSobjectsController" lightningStylesheets="true" showHeader="false" sidebar="false">
    <apex:slds/>
    <apex:form id="form">
        <apex:selectList value="{!selectedObject}" size="1">
            <apex:selectOptions value="{!objectOptions}" />
            <apex:actionSupport reRender="form" event="onchange" />
        </apex:selectList>
    </apex:form>
</apex:page>

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com

 

All Answers

bhanu_prakashbhanu_prakash
Hi Adi,
Mark as best answer, If it resloves !!​​
Controller
public class DisplayListSobjectsController {
    public String selectedObject { get; set; }
    public String selectedField { get; set; }
    public SelectOption[] getObjectOptions() {
        SelectOption[] results = new SelectOption[] { new SelectOption('','-- none --') };
        for(SObjectType sType: Schema.getGlobalDescribe().values()) {
            DescribeSObjectResult res = sType.getDescribe();
            results.add(new SelectOption(res.getName(), res.getLabel()));
        }
        return results;
    }
    public SelectOption[] getFieldOptions() {
        SelectOption[] results = new SelectOption[] { new SelectOption('','-- none --') };
        if(selectedObject != null) {
            for(SObjectField sField: Schema.getGlobalDescribe().get(selectedObject).getDescribe().fields.getMap().values()) {
                DescribeFieldResult res = sField.getDescribe();
                results.add(new SelectOption(res.getName(), res.getLabel()));
            }
        }
        return results;
    }
}
Visualforce Page
<apex:page controller="DisplayListSobjectsController" lightningStylesheets="true" showHeader="false" sidebar="false">
    <apex:slds/>
    <apex:form id="form">
        <apex:selectList value="{!selectedObject}" size="1">
            <apex:selectOptions value="{!objectOptions}" />
            <apex:actionSupport reRender="form" event="onchange" />
        </apex:selectList>
    </apex:form>
</apex:page>

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com

 
This was selected as the best answer
adi salesforceadi salesforce
Hi Bhanu Prakash,

I got the perfect output thank you so much. I need further assistance to this task. Now if I select any object  the picklist fields in that object should come in a drop down. Could you give the code to meet that criteria.
arti rathodarti rathod
Hi bhanu_prakash,

Can i get test class for controller?