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
sumit dsumit d 

dynamic query to retrieve All the field of selected object

Hi All,
         my requirement is to get All the field from a selected object. i have a drop down list of All the object now i want that when i select A object than its field will also show in dropdown list According to the selected object dynamically.User-added imagei have All Account list now i want Field list dynamically According to selected object.
can Anyone help me out?
Asitha_RajuAsitha_Raju
Hi Sumit,
Try this code:
Controller:
public class ObjectFieldExplorerController {
    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="ObjectFieldExplorerController">
    <apex:form id="form">
        <apex:selectList value="{!selectedObject}" size="1">
            <apex:selectOptions value="{!objectOptions}" />
            <apex:actionSupport reRender="form" event="onchange" />
        </apex:selectList>
        <apex:selectList value="{!selectedField}" size="1">
            <apex:selectOptions value="{!fieldOptions}" />
        </apex:selectList>
    </apex:form>
</apex:page>
Hope this helps you. If it solves your issue please mark it as the best answer.
Thanks.​
sumit dsumit d
Hi ,
Thanks for the reply.
i want to know that is it possible to show All the field as checkbox so that i can select and save them in An object like given below:-User-added image plese help me out with this?