+ Start a Discussion
hemant ranahemant rana 

Dynamic retrieval of fields of any sobject in vf page..

Map<String, Schema.SObjectField> M = Schema.SObjectType.Account.fields.getMap();

from this we can retrieve all the fields but of account object only but i want retrieve all the fields of any sobject.

I had accessed the sobjects dynamically by this---

List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
    List<SelectOption> options = new List<SelectOption>();
  list<String> fieldlabel=new list<String>();
   
   GlobalDescribeMap.put(fieldlabel,gd);
  //Schema.getGlobalDescribe();
   for(Schema.SObjectType f : gd)
    {
    if(f.getDescribe().isCreateable() && (!f.getDescribe().CustomSetting) &&  (f.getDescribe().keyPrefix!=null ||f.getDescribe().keyPrefix!='') && f.getDescribe().isUpdateable() && f.getDescribe().isQueryable() && f.getDescribe().isAccessible() && f.getDescribe().isUndeletable())                    
       {
       options.add(new SelectOption(f.getDescribe().getLabel(),f.getDescribe().getLabel()));
       fieldlabel.add(f.getDescribe().getLabel());
      // system.debug('@@@'+fieldlabel);
   }
   }
    return options;



but not able to dynamically retrieve the fields of any sobject .. please help
Best Answer chosen by hemant rana
Swati GSwati G
oops sorry. you will have to do getDescribe();

like this Map<String, Schema.SObjectField> M = f.getDescribe().fields.getMap();

All Answers

Swati GSwati G
Hi,

You can call fields on sobject type as highlighted below:

   for(Schema.SObjectType f : gd)
    {
    if(f.getDescribe().isCreateable() && (!f.getDescribe().CustomSetting) &&  (f.getDescribe().keyPrefix!=null ||f.getDescribe().keyPrefix!='') && f.getDescribe().isUpdateable() && f.getDescribe().isQueryable() && f.getDescribe().isAccessible() && f.getDescribe().isUndeletable())                   
       {
       options.add(new SelectOption(f.getDescribe().getLabel(),f.getDescribe().getLabel()));
       fieldlabel.add(f.getDescribe().getLabel());
      // system.debug('@@@'+fieldlabel);
      Map<String, Schema.SObjectField> M = f.fields.getMap();
   }
   }
hemant ranahemant rana
Hi swati its giving error-----
Error: objectList Compile Error: Expression of type Schema.SObjectType has no member named fields at line 30 column 44



Swati GSwati G
oops sorry. you will have to do getDescribe();

like this Map<String, Schema.SObjectField> M = f.getDescribe().fields.getMap();
This was selected as the best answer
hemant ranahemant rana
Swati Thanks a lot.... its working perfectly :-)