3

The Following works good to retrieve my account list in a SelectList on my VF.
But I'm trying to set a specific Default Account as the first value in the list - Now it shows up last..
*Is there any way to choose a specific Value (based on any Criteria I choose) from my account List and set as Default on my SelectList ?? *

Controller :

public List<Account> matchingAccounts          = new List<Account>();
public List<SelectOption> accountSelectOptions = new List<SelectOption>();
public String matchAccountId                 {get;set;}
public String accountName                    {get;set;}

public List<SelectOption> getAccountSelectOptions()     { 
    matchingAccounts = [Select Id,Name From Account WHERE Name LIKE : accountName];
    if(matchingAccounts!=null) {
         for(Account acc:matchingAccounts){
             if(acc.myDefault__c) {
             accountSelectOptions.add(new SelectOption(acc.Id, acc.Name));

             //Setting as Default Value - FIXED...
             matchAccountId =  acc.Id;
             }
             else {
              accountSelectOptions.add(new SelectOption(acc.Id, acc.Name));
             }
            } 
    else {
         accountSelectOptions.add(new SelectOption('New','New Account: '+pot.Account_Name__c));
          }
    return accountSelectOptions;
  }

VF Page :

<apex:selectList label="Account Name" value="{!matchAccountId}" size="1" required="true">
      <apex:selectOptions value="{!accountSelectOptions}"/>
      <apex:actionSupport event="onchange" reRender="thePageBlock" action="{!showHideRTSelect}"/>
</apex:selectList>
  • Please see this other question: developer.salesforce.com/forums/?id=906F00000008yttIAA – techbusinessman Jul 26 '15 at 2:29
  • (1) I'm surprised this VF page compiled - matchAccountId should be public; (2) the if-else in the for loop seems unnecessary as coded; (3) matchingAccounts will never be null as it is instantiated from a SOQL call and hence will be a list, possibly empty – cropredy Jul 26 '15 at 3:57
  • Sorry late at night and I copied the playground code Not the actual so might be some mistakes.. (1) fixed it you are correct should be public since I need to call it from my VF (2) if-else was done in order to choose which value will be default (3) 'matchingAccounts' can be null as it always finds similar accounts by a given Name. – sfdx bomb Jul 26 '15 at 7:05
4

Yes, set the default value on matchAccountId. The Select List should initialize with that value as selected.

  • True.. I have fixed the code above according to your answer - I think i it should work fine now. matchAccountId default Value was set inside if-else statement.. – sfdx bomb Jul 26 '15 at 7:11
0

For Visualforce there is a hacky solution available aswell:

<apex:selectOption value="foo" html-selected="true"/>
<apex:selectOption value="bar" />

The html-selected="true" is being set directly on the html option tag. In HTML selected is used to define the standard option. Note that is normally does not have an asigned value (="true")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.