+ Start a Discussion
satish yagana 4satish yagana 4 

where i need a pick list on my VF page , picklist contains all objects. based on this pick list value i want to display all fields of appropriate object. for example if i select Account from pick list then it should show all its fields

i have some requirement where i need a pick list on my VF page , pick  
   list contains all  objects.   based on this pick list value i want to  
   display all fields of appropriate object. for example if i select  
   Account from  pick list then it should show all its fields in the  
   page. please tell me how to do this & send some sample code 
Best Answer chosen by satish yagana 4
DeveloperSudDeveloperSud
Hi,

I dont know what is the pupose of doing like this.You can achieve it by using the below code but u need to hit eset button each time u want to change the object name in the list.
 
<apex:page controller="sample1xxx"> 
 <apex:form >
     <apex:pageBlock >
      <apex:pageBlockSection title="Choose The Object From Here" >
       <apex:pageBlockSectionItem >
        <apex:selectList size="1" value="{!selectedObject}" id="myPicklist"   >
         <apex:selectOptions value="{!nameOfObjects}" />
         <apex:actionsupport event="onchange" reRender="pb1" action="{!showFields}" /> 
        </apex:selectList>
       </apex:pageBlockSectionItem>
      </apex:pageBlockSection>  
     </apex:pageBlock>
     
     <apex:pageBlock id="pb1"  >
     <apex:pageBlockSection columns="3" title="Show the fields">
       <apex:repeat value="{!newNameOfFields}" var="f">
        <apex:inputField value="{!objectName[f]}"/>
       </apex:repeat>
     </apex:pageBlockSection> 
     <apex:pageBlockSection >   
     </apex:pageBlockSection> 
      <apex:commandButton value="Reset" action="{!cancel}" immediate="true" />
     </apex:pageBlock>
     
    </apex:form>    
</apex:page>
 
public with sharing class sample1xxx {

  public list<selectoption> nameOfObjects{get;set;}
  public map<string,schema.SobjectType> mapOfTokens{get;set;}
  public List<String> listToHoldNames{get;set;}
  public string selectedObject{get;set;}
  
  public Sobject objectName{get;set;}
  public List<Sobject> objectNames{get;set;}
  public List<Sobject> objects{get;set;}
  public List<String> fields{get;set;}
  public String query{get;set;}
  public List<string> newNameOfFields{get;set;}
  
  public sample1xxx(){

  mapOfTokens=Schema.getGlobalDescribe();
  listToHoldNames= new list<string>();
  nameOfObjects= new list<selectoption>();
  listToHoldNames.addAll(mapOfTokens.keyset());
  listToHoldNames.sort();
  nameOfObjects.add(new selectoption('None','--None--'));
  for(string s:listToHoldNames){
  nameOfObjects.add(new selectoption(s,s));
  newNameOfFields= new List<String>();
  }
  fields = new  List<String>();
  objectNames=new List<Sobject>();
  }
  
  public pagereference showFields(){
  
  system.debug('The selectedObject is :'+selectedObject);
  schema.describeSobjectResult dsr=mapOfTokens.get(selectedObject).getDescribe();
  Map<string,Schema.SobjectField> mapOfFields=dsr.fields.getmap();
  list<String> nameOfFields= new List<String>();
  nameOfFields.addAll(mapOfFields.keySet());
  for(String so:nameOfFields){
  schema.describeFieldResult arbDfr=mapOfFields.get(so).getDescribe();
  if(arbDfr.Createable==true){
  newNameOfFields.add(so);
  }
  }
  schema.SobjectType obj= schema.getGlobalDescribe().get(selectedObject);
  system.debug('obj is :'+obj);
  objectName=obj.newSobject();
  objectNames.add(objectName);
  system.debug('Object Name is :'+objectName);
  return null;
  
  }
  
  public pageReference save(){
  insert objectNames;
  return null;
  
  }
  
  public pageReference cancel(){
  pageReference pg=new PageReference(ApexPages.currentPage().getURL());
  pg.setRedirect(true);
  return pg;
  }
  
}

Do not forget to mark it as solved if this meets your requirement.

All Answers

DeveloperSudDeveloperSud
Hi ,

You can try sething like below.
 
<apex:page Controller="sample1zzz">
<apex:form id="Describe">
<apex:pageBlock id="block2" >
<apex:pageblockbuttons location="top" >
<apex:commandButton value="Get Describe Object Fields" action="{!showFields}"/>
</apex:pageblockbuttons>
<apex:pageblocksection >
<apex:selectList value="{!selectedObject}" size="1">
<apex:selectOptions value="{!objectNames}"/>
</apex:selectList>
</apex:pageblocksection>
<apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">
<apex:panelBar items="{!fields}" var="fls">
<apex:panelBarItem label="{!fls.key}">{!fls.val}</apex:panelBarItem>
</apex:panelBar>
</apex:pageblocksection>
</apex:pageBlock>
</apex:form>
</apex:page>
 
public with sharing class sample1zzz {

public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public List<Pair> lstfieldname{get;set;}
public List <Pair> fields {get{return lstfieldname;} set{lstfieldname =value;}}
public List <SelectOption> objectNames{public get; private set;}
public String selectedObject {get; set;}

// Intialize objectNames and fields

public sample1zzz() {
objectNames = initObjNames();
fields = new List<Pair>();
}
// Populate SelectOption list -

// find all sObjects available in the organization

private List<SelectOption> initObjNames() {
List<SelectOption> objNames = new List<SelectOption>();
List<String> entities = new List<String>(schemaMap.keySet());
entities.sort();
objNames.add(new SelectOption('None','--NONE--'));
for(String name : entities)
objNames.add(new SelectOption(name,name));
return objNames;
}

// Find the fields for the selected object

public void showFields() {
//fields.clear();
system.debug('$$$$$' + selectedObject);
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
system.debug('#######' + dfield );
Pair field = new Pair();
field.key = dfield.getname();
system.debug('#######4444' + field.key);
field.val = dfield.getType () + ' : ' + dfield.getLabel ();
lstfieldname.add(field);
}
}

public class Pair
{
public String key {get; set;}
public String val {get; set;}
}
}

Do not forget to mark this as solved if this meets your requirement.
satish yagana 4satish yagana 4
not like this ...if i select the object then corresponding fields shoud be appear like this...i'm attaching imageUser-added image
satish yagana 4satish yagana 4
hi .., many thanks for your response but my requirement is that the fields are display not as pick list..im attaching image for my requirement.
DeveloperSudDeveloperSud
Hi,

I dont know what is the pupose of doing like this.You can achieve it by using the below code but u need to hit eset button each time u want to change the object name in the list.
 
<apex:page controller="sample1xxx"> 
 <apex:form >
     <apex:pageBlock >
      <apex:pageBlockSection title="Choose The Object From Here" >
       <apex:pageBlockSectionItem >
        <apex:selectList size="1" value="{!selectedObject}" id="myPicklist"   >
         <apex:selectOptions value="{!nameOfObjects}" />
         <apex:actionsupport event="onchange" reRender="pb1" action="{!showFields}" /> 
        </apex:selectList>
       </apex:pageBlockSectionItem>
      </apex:pageBlockSection>  
     </apex:pageBlock>
     
     <apex:pageBlock id="pb1"  >
     <apex:pageBlockSection columns="3" title="Show the fields">
       <apex:repeat value="{!newNameOfFields}" var="f">
        <apex:inputField value="{!objectName[f]}"/>
       </apex:repeat>
     </apex:pageBlockSection> 
     <apex:pageBlockSection >   
     </apex:pageBlockSection> 
      <apex:commandButton value="Reset" action="{!cancel}" immediate="true" />
     </apex:pageBlock>
     
    </apex:form>    
</apex:page>
 
public with sharing class sample1xxx {

  public list<selectoption> nameOfObjects{get;set;}
  public map<string,schema.SobjectType> mapOfTokens{get;set;}
  public List<String> listToHoldNames{get;set;}
  public string selectedObject{get;set;}
  
  public Sobject objectName{get;set;}
  public List<Sobject> objectNames{get;set;}
  public List<Sobject> objects{get;set;}
  public List<String> fields{get;set;}
  public String query{get;set;}
  public List<string> newNameOfFields{get;set;}
  
  public sample1xxx(){

  mapOfTokens=Schema.getGlobalDescribe();
  listToHoldNames= new list<string>();
  nameOfObjects= new list<selectoption>();
  listToHoldNames.addAll(mapOfTokens.keyset());
  listToHoldNames.sort();
  nameOfObjects.add(new selectoption('None','--None--'));
  for(string s:listToHoldNames){
  nameOfObjects.add(new selectoption(s,s));
  newNameOfFields= new List<String>();
  }
  fields = new  List<String>();
  objectNames=new List<Sobject>();
  }
  
  public pagereference showFields(){
  
  system.debug('The selectedObject is :'+selectedObject);
  schema.describeSobjectResult dsr=mapOfTokens.get(selectedObject).getDescribe();
  Map<string,Schema.SobjectField> mapOfFields=dsr.fields.getmap();
  list<String> nameOfFields= new List<String>();
  nameOfFields.addAll(mapOfFields.keySet());
  for(String so:nameOfFields){
  schema.describeFieldResult arbDfr=mapOfFields.get(so).getDescribe();
  if(arbDfr.Createable==true){
  newNameOfFields.add(so);
  }
  }
  schema.SobjectType obj= schema.getGlobalDescribe().get(selectedObject);
  system.debug('obj is :'+obj);
  objectName=obj.newSobject();
  objectNames.add(objectName);
  system.debug('Object Name is :'+objectName);
  return null;
  
  }
  
  public pageReference save(){
  insert objectNames;
  return null;
  
  }
  
  public pageReference cancel(){
  pageReference pg=new PageReference(ApexPages.currentPage().getURL());
  pg.setRedirect(true);
  return pg;
  }
  
}

Do not forget to mark it as solved if this meets your requirement.
This was selected as the best answer
satish yagana 4satish yagana 4
hii.., thank you very much for your help.. this is the best solution..
DeveloperSudDeveloperSud
Hi ,

Glad to hear it works.Kindly mark this as Solved.Thanks!!!
Stelian Stefan 2Stelian Stefan 2
Hi, 
How to retrive data based on the chosen picklist? Example: show me contacts with skills from the dropdown. 
Your help is appreciated