Schema Programming Scenario

Schema Programming Scenarios

Scenario 1
Get all the sObjects from your Salesforce Org and display the same as picklist in VF page.
1. Use getGlobalDescribe() and store the objectnames and tokens into a map.
2. From the map, get the list of object names.
3. Build list of selectOption variable

Use selectList and selectOptions to get the list of object names.

Apex Code

public with sharing class SchemaController1 {
    public List<selectOption> options  {get; set;}

    public SchemaController1(){
        options = new List<selectOption>();
        Map<String, Schema.sObjectType> mapObjects =  Schema.getGlobalDescribe(); 
        List<String> ObjNames = new List<String>(mapObjects.keySet()); 
        for(String s: objNames){
            selectOption op = new selectOption(s, s);

Visualforce Code

<apex:page controller="SchemaController1">
    <apex:form >
        <apex:pageblock >
            <apex:pageBlockSection >
                <apex:pageblockSectionItem >
                    <apex:outputLabel value="Objects"></apex:outputLabel>
                    <apex:selectList size="1">
                        <apex:selectOptions value="{!options}"></apex:selectOptions>

You may get the output as below (the list will have all the sObjects from the Org. The screen shot showing only few objects for reference here)


Scenario 2:
1. Create picklist with list of all sObject Names
2. When user selects an option, then display all the properties (label, readAccess, isCustom) of sObject.
actionsupport (event, action) inside selectList

Apex Code

public class SchemaDescribeDemo1 {
    public Map<String, Schema.SobjectType>     objMap	 {set; get;}
    public List<SelectOption> 		       options   {set; get;}
    public String 			       selected	 {set; get;}
    public SobjectResult1 		       result    {set; get;}
    public SchemaDescribeDemo1(){
        objMap  = Schema.getGlobalDescribe();
        options = new List<SelectOption>();
        Set<String> keys = objMap.keyset();
        List<String> objNames = new List<String>(keys);
        SelectOption n=new SelectOption('none','-None-');
        for(String s:objNames){
           SelectOption op=new SelectOption(s,s);
    public void getDetails(){
            Schema.SobjectType obj = objMap.get(selected);
            Schema.DescribeSobjectResult res = obj.getDescribe();
            result = new SobjectResult1();
            result.labelName = res.getLabel();
            result.objectName = res.getName();
            result.createAccess = res.isCreateable();
            result.updateAccess = res.isUpdateable();
            result.customObject = res.isCustom();
            result.readAccess = res.isAccessible();
    public class SobjectResult1{
        public string labelName       {set; get;}
        public string objectName      {set; get;}
        public Boolean createAccess   {set; get;}
        public Boolean updateAccess   {set; get;}
        public Boolean customObject   {set; get;}
        public Boolean readAccess     {set; get;}

Visualforce Code

<apex:page controller="SchemaDescribeDemo1">
    <apex:form >
    	<apex:pageBlock title="SchemaSobject">
        	<apex:pageBlockSection columns="1">
            	    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Objects" />
                        <apex:selectList size="1" value="{!selected}">
                            <apex:selectOptions value="{!options}" />
                            <apex:actionSupport event="onchange" action="{!getDetails}" />
                <apex:pageBlocksectionItem >
                	<apex:outputPanel rendered="{! !ISNULL(result)}">
                            Label Name : {!result.LabelName} <br/>
                            Object Name: {!result.objectName} <br/>
                            Read Access: {!result.readAccess} <br/>
                            Update Access:{!result.updateAccess}

Scenario 3:
1. Create picklist with list of all sObject Names
2. When user selects an option, then display all the fields of that object in the form of picklist.