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
Hero285LinkHero285Link 

Show my Account.Name as a Picklist o search in my VFP

Hello everyone, 

Im new In this Environment and I had been stuck for several hours,,

my question is...

Is it possible to take my field Name(Text), From my object Account,, and display it on my Visual Force Page as a PickList or as a SearchPanel, where the user could easily choose a predefined Name from Account???

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

Hi,

Yes . you can do this. you can easily display account name as a picklist ,SelectOption is used in salesforce to create custom picklist.

 

------------------------------------Apex ------------------------

Public Class AccountController {

      public String selectedAccId{get;set;}

       

       public List<SelectOption> getAccountNames() {

              List<SelectOption> accOptions= new List<SelectOption>();

              accOptions.add( new SelectOption('','--Select--'));

              for( Account acc : [select Id,name from Account ] ) {

                      accOptions.add( new SelectOption(acc.Id,acc.name)); /*SelectOption list takes two parameters one is value and other one is label .In this case account name as a label and Id is the value .*/

              }

             return accOptions;

       }

}

 

 

---------------------------------Visual Force -----------------------

<apex:page controller="AccountController1">
          <apex:form>
                   <apex:pageBlock title="Account Name">
                               <apex:selectList value="{!selectedAccId}" size="1"> <!-- selectedAccId store the selected label value as account Id of selected account name.-->
                                           <apex:selectOptions value="{!AccountNames}" />
                               </apex:selectList>
                  </apex:pageBlock>
        </apex:form>

</apex:page>

 

 

Please let me know if u have any problem on same and if this post helps u plz give KUDOS by clcik on star at left.

All Answers

Puja_mfsiPuja_mfsi

Hi,

Yes . you can do this. you can easily display account name as a picklist ,SelectOption is used in salesforce to create custom picklist.

 

------------------------------------Apex ------------------------

Public Class AccountController {

      public String selectedAccId{get;set;}

       

       public List<SelectOption> getAccountNames() {

              List<SelectOption> accOptions= new List<SelectOption>();

              accOptions.add( new SelectOption('','--Select--'));

              for( Account acc : [select Id,name from Account ] ) {

                      accOptions.add( new SelectOption(acc.Id,acc.name)); /*SelectOption list takes two parameters one is value and other one is label .In this case account name as a label and Id is the value .*/

              }

             return accOptions;

       }

}

 

 

---------------------------------Visual Force -----------------------

<apex:page controller="AccountController1">
          <apex:form>
                   <apex:pageBlock title="Account Name">
                               <apex:selectList value="{!selectedAccId}" size="1"> <!-- selectedAccId store the selected label value as account Id of selected account name.-->
                                           <apex:selectOptions value="{!AccountNames}" />
                               </apex:selectList>
                  </apex:pageBlock>
        </apex:form>

</apex:page>

 

 

Please let me know if u have any problem on same and if this post helps u plz give KUDOS by clcik on star at left.

This was selected as the best answer
Hero285LinkHero285Link

Thank You Sr. It was really helpful ! =D

vinayk kumarvinayk kumar
thank you@Puja_mfsi.............):
Sathya MithranSathya Mithran
in my case I m getting label instead of id in the in the debug for selectedAccId. I have similar process like this