function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
divyanshudivyanshu 

how to get fields using apex

Hi Everyone,

 

how to get all the objects with their  fields using apex page and class.

 

like,if i select lead from the dropdown menu then its shows all the lead fields and if i select oppurtunity then its show all the oppurtunity fields.

 

Please help me on this

 

as i am new to the salesforce

 

Thanxs in advance

 

Regards

Divyanshu

super developersuper developer

For this you need to write code.. for this you need to learn apex code classes and vf pages

divyanshudivyanshu

HI,

 

below is my page and class code.As i am able to retrieve  all the object from my organization but not able to find the fields while selecting the anyone of them.

Page code:

<apex:page controller="objectList"  showHeader="false" sidebar="false">
      <apex:form >
        <apex:SelectList value="{!val}" size="1">
          <apex:selectOptions value="{!Name}"></apex:selectOptions>
        </apex:SelectList>
      </apex:form>
    </apex:page>

CLASS code:

public class objectList{
      public String val {get;set;}
      
      public List<SelectOption> getName()
      {
        List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();    
        List<SelectOption> options = new List<SelectOption>();
         
        for(Schema.SObjectType f : gd)
        {
           options.add(new SelectOption(f.getDescribe().getLabel(),f.getDescribe().getLabel()));
        }
        return options;
       }

   

}

 

Any help will be apreciated



Raumil SetalwadRaumil Setalwad

Hello friend

For displaying list of fields of selected object use follwoing code

For class
public List<SelectOption> getCateg(){
 List<SelectOption> options = new List<SelectOption>();
 Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
 Map<String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
 options.add(new SelectOption('Select Field', '-- Select Fields --')); --> First value
 for (String fieldName: fieldMap.keySet()) {
        options.add(new SelectOption(fieldName, fieldName));  } 
          return options;
}


In page 
<apex:selectList value = {!flds} size = "1" >
 <apex:selectOption value {!Categ}/>
<apex:selectList> 

 Hope this helps

Regards

Raumil Setalwad

 

divyanshudivyanshu

HI

Thanxs for your reply.

I know these are the stupid question but i am not able to clearly myself.

 

Select Fields means.and what to pass in field name.

 

Will u tell me is my code is correct ar not??

 

Thanxs alot.

 

Please do reply as i wanted to clearify my concepts.

 

Raumil SetalwadRaumil Setalwad

Hello Friend,

I have already mentioned in my previous post when the page is loaded value 'Select Field' will be load as default. as soon as you select object list of its will be displayed in next picklist. Also to mention that according to my functionality i had to use it you can ignore it.

 

Also the code which is mentioned by you is corect according to my knowledge of describe calls it should populate llist of objects