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.
Apex
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

Visualforce
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()); 
        objNames.sort();
        
        for(String s: objNames){
            selectOption op = new selectOption(s, s);
            options.add(op);
        }                         
    }
}

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>
                    </apex:selectList>
                </apex:pageblockSectionItem>
            </apex:pageBlockSection>    
        </apex:pageblock>
    </apex:form>
</apex:page>

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)

sobject

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);
        objNames.sort();
        SelectOption n=new SelectOption('none','-None-');
        options.add(n);
        
        for(String s:objNames){
           SelectOption op=new SelectOption(s,s);
           options.add(op);
        }
    }
    public void getDetails(){
        if(selected!='none'){
            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:selectList>
                    </apex:pageBlockSectionItem>
                <apex:pageBlocksectionItem >
                	<apex:outputPanel rendered="{! !ISNULL(result)}">
                            Label Name : {!result.LabelName} <br/>
                            Object Name: {!result.objectName} <br/>
                            Read Access: {!result.readAccess} <br/>
                            CreateAccess:{!result.createAccess}<br/>
                            Update Access:{!result.updateAccess}
                        </apex:outputPanel>
                </apex:pageBlocksectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

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.