Sunday, May 24, 2015

Schema Programming in Apex and its usage in Visualforce Page.

Schema Programming in Apex and its usage in Visualforce Page.


Schema give the meta data information about the data (Object, Fields).

Schema Methods :

1). public static Map<String, Schema.SObjectType> getGlobalDescribe()
   
 This method returns a map of all the Sobject names as keys and Sobject val tokens as Values.


Ex:

Display all the list of objects available in the salesforce organization in the Visualfoce page.


Controller Class:

public class SchemaDescribeExample {
    public List<SelectOption> options;
    
    public list<SelectOption> getOptions(){
        return options;
    }
    
    public SchemaDescribeExample(){
        options = new List<SelectOption>();
        Map<String, Schema.SObjectType> schemaMap = schema.getGlobalDescribe();
        Set<String> objectSet = schemaMap.keySet();
        for(String str:objectSet){
            //praparing label and values in selection option.
            SelectOption op =new SelectOption(str,str);
            options.add(op);
        }
    }
    
    

}


VF Page:

<apex:page controller="SchemaDescribeExample">
 <apex:form >
  <Apex:selectList size="1">
    <apex:selectOptions value="{!options}"></apex:selectOptions>
   
  </Apex:selectList>
 </apex:form>
</apex:page>



That's it.. now it will display the list of all the objects in picklist.


More about Dynamic Apex:



Schema Programming in Apex and its usage in Visualforce Page.




0 comments:

Post a Comment

 
| ,