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
Abhishek KedariAbhishek Kedari 

Showing predefined custom fields data on visualforce page.

Hi All,

    I have an objecte created with custom field Gender (type is pick list and contains 2 values Male and Female). Now I want to show this values on my visualforce page.
   Can anyone please tell me how I can do that ?

   When I searched on internet i got this piece of code :
<apex:selectList value="{!countries}" multiselect="true">
            <apex:selectOptions value="{!items}"/>
        </apex:selectList><p/>
but I only know my object name and field name. Not sure how to use above.
For example suppose my object name is 'Information' and picklist custom field is 'Gender' How I can write this in above snippest.

Also after that I want to send selected value to apex controller.

Thanks in advance.


Best Regards,
Abhishek 

   

Best Answer chosen by Abhishek Kedari
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi Abhishek,

Please use the following code:

Page:
**********

<apex:page controller="AjaxController">
<apex:form >
<apex:outputLabel >Gender</apex:outputLabel>
<apex:selectList size="1">
    <apex:selectOptions value="{!lstGender}"></apex:selectOptions>
</apex:selectList>
</apex:form >
</apex:page>


Controller:
*******************


public class AjaxController {

    public selectOption[] lstGender { get; set; }
   
   
    public AjaxController(){
   
       lstGender = new selectoption[]{};      
     
      
       Schema.describeFieldresult  f1 = Account.Gender__c.getdescribe();//You can use any object in place of account

       for(Schema.picklistEntry p1 :f1.getPicklistValues()){
      
           lstGender.add(new selectOption(p1.getLabel(),p1.getLabel()));   
       }
    }

This works for you. dont forget to mark it as solution.

Regards,
Vijay

All Answers

James LoghryJames Loghry
Depending on how complicated your logic is in your controller, you could get away with something as simple as using the standard controller, along with the picklist field and the save method in your standard controller.  An example of this is below:

<apex:page standardController="Information__c">
    <apex:form>
    <apex:inputField value="{!Information__c.Gender__c}" />
    <apex:commandButton value="Save" action="{!save}" />
    </apex:form>
</apex:page>

You can read more about Standard controllers here: https://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_std.htm

I
f you're looking at anything custom, you need to consider a combination of standard controller and a custom extension controller.

Your Visualforce page may look like the following (not much different here other than the extensions attribute:
<apex:page standardController="Information__c" extensions="InformationExtCtrl">
    <apex:form>
    <apex:inputField value="{!Information__c.Gender__c}" />
    <apex:commandButton value="Save" action="{!save}" />
    </apex:form>
</apex:page>

Your apex may look like the following:

public with sharing class InformationExtCtrl{

    public Information__c information {get; set;}

    //Standard constructor called when this class is used as an extension
    public InformationExtCtrl(ApexPages.StandardController ctrl){
        information = (Information__c)ctrl.getRecord();

        //Might need to add additional declarations here
    }

    //Overrides the standard save method of the Standard Controller.
    public PageReference save(){
        //Custom logic for saving the record;
   
        //Perform some DML on information (insert, upsert, update, delete)
        upsert information;
    }
}

For more info on extension controllers, see here: https://www.salesforce.com/us/developer/docs/pages/Content/pages_controller.htm

N
ote, the above example will work for Picklist fields on an sobject, but if you need to create a custom select list like the one in the example you found, you're looking at something slightly more complex than the above example I posted.
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi Abhishek,

Please use the following code:

Page:
**********

<apex:page controller="AjaxController">
<apex:form >
<apex:outputLabel >Gender</apex:outputLabel>
<apex:selectList size="1">
    <apex:selectOptions value="{!lstGender}"></apex:selectOptions>
</apex:selectList>
</apex:form >
</apex:page>


Controller:
*******************


public class AjaxController {

    public selectOption[] lstGender { get; set; }
   
   
    public AjaxController(){
   
       lstGender = new selectoption[]{};      
     
      
       Schema.describeFieldresult  f1 = Account.Gender__c.getdescribe();//You can use any object in place of account

       for(Schema.picklistEntry p1 :f1.getPicklistValues()){
      
           lstGender.add(new selectOption(p1.getLabel(),p1.getLabel()));   
       }
    }

This works for you. dont forget to mark it as solution.

Regards,
Vijay
This was selected as the best answer
Abhishek KedariAbhishek Kedari
Thanks James and Vijay for helping me out.

Regards,
Abhishek