Tuesday 12 April 2016

List of Fields based on the object what we selected

Once you select the object and click on GetProperties button  you will get list of corresponding fields in the object

public  class SchemaFields {
     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 List<SelectOption> fldoptions{Set;get;}
     public SchemaFields(){
          objects=Schema.getGlobalDescribe();
          options=new List<selectOption>();
          objNames=new List<String>();
          fldoptions=new List<SelectOption>();
          SelectOption p=new SelectOption('none','-None-');
          fldoptions.add(p);
          options.add(p);
          Set<String> keys=objects.keySet();
          objNames.addAll(keys);
          objNames.sort();
          for(String s:objNames){
              Schema.DescribeSobjectResult result=objects.get(s).getDescribe();
              if(result.isCustom()){
                   SelectOption op=new SelectOption(s,s);
                   options.add(op);
              }
          }
     }
     public void getDetails(){
          fldoptions.clear();
          Schema.DescribeSobjectResult result=objects.get(selected).getDescribe();
          Map<String,Schema.SobjectField> fldmap=result.fields.getMap();
          List<String> flds=new List<String>();
          Set<String> keys=fldmap.keySet();
          flds.addAll(keys);
          flds.sort();
          for(String s:flds){
              SelectOption op=new SelectOption(s,s);
              fldoptions.add(op);
          }
     }
    

}

Visualforce  :
<apex:page  controller="SchemaFields">
     <apex:form>
     <apex:pageBlock title="Schema of Object with fields" >
          <apex:panelGrid columns="3" id="one" style="width:90%">
              <apex:selectList value="{!selected}" size="1">
                   <apex:selectOptions value="{!options}" />
              </apex:selectList>
              <apex:commandButton value="GetProperties" action="{!getDetails}"  reRender="sl2" />
              <apex:selectList  size="1" id="sl2">
                   <apex:selectOptions value="{!fldoptions}" />
              </apex:selectList>
          </apex:panelGrid>
     </apex:pageBlock>
     </apex:form>

</apex:page>

No comments:

Post a Comment