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>
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