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
gdkgdk 

How to add field names of an object into picklist on VF-Page

HariDineshHariDinesh

Hi,

 

You can use Schema.SobjectType to get the describe information about object.

By this you can get all the information regarding the object.

 

// Declare prop to store the selected value
public String selfld{get;set;}
// Declare a property variable in calss
public List<SelectOption> objFields{get; set;} 
// create memore for this like below in mehtod
objFields =new List<SelectOption>();
// add none if needed in Picklist.
objFields.add(new SelectOption('-1','--None--')); 

write below code
Map<String,Schema.SObjectType> ssot = Schema.getGlobalDescribe();
Schema.SObjectType sobjType = ssot.get('objectname__c');
Schema.DescribeSObjectResult dsr = sobjType.getDescribe();
Map<String,Schema.SObjectField> obj = dsr.fields.getMap(); 
for(String fieldName : obj.keyset())  
 {  
   Schema.SObjectField field = obj.get(fieldName);                                          Schema.DescribeFieldResult fieldDesc = field.getDescribe();  
   objFields.add(new SelectOption(fieldName.toLowerCase(),fieldName.toLowerCase()));
   objFields.sort();  
  }

 

 

The VFP code for this picklist:

 

<apex:pageBlockSection id="pbid" title="title" columns="2">
                    <apex:pageBlockSectionItem id="pbsitem">
                        <apex:outputText value="object name"/>  
                        <apex:SelectList id="selde" size="1" value="{!selfld}">
                        <apex:selectOptions value="{!objFields}"/>
                        </apex:SelectList>                    
                    </apex:pageBlockSectionItem>
                                
            </apex:pageBlockSection>

 

Mark it as Solution if it Answers your Question.

Daniel B ProbertDaniel B Probert
hi there just came across this code and am trying to use it but getting an error:

Compile Error: unexpected token: '=' at line

which is related to

objFields =new List<SelectOption>();

am i missing something i've just done a copy paste of what your sent.