Sunday 21 February 2016

Schema

Schema.getGlobalDescribe() : This method will return  Map<String,Schema.SobjectType>
Where string is the object Name and Schema.SobjectType is the sobject
Display the List of objects in the salesforce org as picklist options
When you select any one of the object and clik on get Properties it should display  the properties of the object.


To get the properties of the object
Scheama.DescribeSobjectResult result=Account.SobjectType.getDescribe();

Class :
public  class SchemaExample {
     public Map<String,Schema.SobjectType> objects {set;get;}
     public List<SelectOption> options {set;get;}
     public List<String> objNames{set;get;}
     public String selected {set;get;}
     public String describe {Set;get;}
     public SchemaExample(){
          objects=Schema.getGlobalDescribe();
          options=new List<selectOption>();
          objNames=new List<String>();
          Set<String> keys=objects.keySet();
          objNames.addAll(keys);
          objNames.sort();
          for(String s:objNames){
              SelectOption op=new SelectOption(s,s);
              options.add(op);
          }
    
     }
     public void getDetails(){
          Schema.DescribeSobjectResult result=objects.get(selected).getDescribe();
          describe=''+result;
     }

}

VFPage:
<apex:page  controller="SchemaExample">
     <apex:form>
          <apex:panelGrid columns="3" id="one">
              <apex:selectList value="{!selected}" size="1">
                   <apex:selectOptions value="{!options}" />
     <apex:actionSupport event="onchange"  action="{!getDetails}" reRender="one" />
              </apex:selectList>
              <apex:commandButton value="GetProperties" action="{!getDetails}" />
              <apex:inputTextArea value="{!describe}" rows="5" cols="70" />
          </apex:panelGrid>
     </apex:form>

</apex:page>

No comments:

Post a Comment