An effort to bring a revolution in Salesforce.

Friday, 17 July 2015

How to Dynamically get SObjects & Fields - Salesforce

 public Class  Mysobject{
      public List<SelectOption> options{get;set;}
      public List<SelectOption> Fieldoptions{get;set;}
    
    
      public Mysobject()
        Fieldoptions= new List<SelectOption>();
      }

      public List<SelectOption> getSobject(){
        List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();  
        options = new List<SelectOption>();
        Fieldoptions = new List<SelectOption>();
        for(Schema.SObjectType f : gd){
           options.add(new SelectOption(f.getDescribe().getname(),f.getDescribe().getLabel()));
        }
        return options;
      }
//  Here this method is use to get fields of selected Sobject.
    private List<SelectOption> GeField(String sobj){
     // Sobj is a API Name Of Sobject
        Map<String,Schema.SObjectType> fMap= Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = fMap.get(sobj).getDescribe().fields.getMap();
        if (fieldMap.size() > 0)
            for(Schema.SObjectField sfield : fieldMap.Values()){
            Fieldoptions.add(new SelectOption(sfield .getDescribe().getname(),sfield.getDescribe().getLabel()));
            }
        return Fieldoptions;
      }


     
, ,

No comments:

Post a Comment

Get Gmail, Docs, Drive, and Calendar for business