Get All Objects and their fields into picklist using Schema Programming Salesforce Visualforce page

By | December 13, 2019

Hi, Here we are going to learn about how to retrieve all objects in salesforce into picklist and showing their fields in another picklist using schema programming in visualforce.

Apex Controller

public class Schemaobjfieldsdisppicklist {
    public map<String,Schema.SObjectType> objmap {set;get;}
    public list <selectoption> objects {set;get;}
    public list <selectoption> fields {set;get;}
    public String Selected {set;get;}
    public  Schemaobjfieldsdisppicklist (){
        objects = new list <selectoption>();
        fields = new list <selectoption>();
        selectoption n =new Selectoption('None' ,'-None-');
        objects.add(n);
        fields.add(n);
        //fetch all object from the salesforce metedata
        objmap = Schema.getGlobalDescribe();
        // fetch all the object names from the Map
        set<String> keys =objmap.keyset();
        // create a list to Store Object names And store them
        List<String> objnames = new list<String> (Keys);   
        objnames.sort();
        // take one by one objectname and create them  as piklist option
        for(String s : objnames){
            selectoption op = new selectoption(s,s);
            objects.add(op);
        }
    }
    public void getdata(){
        fields.clear();
        selectoption n =new Selectoption('None' ,'-None-');
        if(selected !='None'){
            schema.SObjectType obj =objmap.get(Selected);   
            //get the description of the give Sobject
            Schema.DescribeSObjectResult result = obj.getDescribe();
            map<String,Schema.SObjectField> fldmap = result.fields.getmap();
            set<String> keys =fldmap.keyset();
            List<String> fldnames = new list<String> (Keys);   
            fldnames.sort();
            for(String s: fldnames){
                Selectoption p = new selectoption(s,s);    
                fields.add(p);
            }
            
        }else{
            fields.add(n);           
            
        }
    }
}

Vf Page

<apex:page controller="Schemaobjfieldsdisppicklist">
    <apex:form >
        <apex:pageBlock title="Schema">
            <apex:pageBlockSection id="one">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Objects"/>                                                                                                                                                                                                                                                                                                                             
                    <apex:selectList size="1" value="{!selected}">
                        <apex:selectOptions value="{!objects}"/>
                        <apex:actionsupport event="onchange" action="{!getdata}" rerender="one"/></apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="fields"/>
                    <apex:selectList size="1" >
                        <apex:selectOptions value="{!fields}"/>
                    </apex:selectList></apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output


Leave a Reply

Your email address will not be published. Required fields are marked *