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
HemanthrulezHemanthrulez 

URGENT: Get Values from PickList(Multi Select) using SOQL

Hi,

 

I have createed a custom Field Country__c  in User Object with 10 values in it. How do I retrieve those values using SOQL in the controller and populate in <apex:selectOptions>. All those items must be displayed on the visualforce page so that I can make multiple selections.

 

 

Controller:

 

public class MyMSLController {
    String[] countries = new String[]{};
    User userObj = null;
    public MyMSLController(ApexPages.StandardController controller) {
    userObj = (User)controller.getRecord();
    
    
    }
public String[] getCountries()
{
return countries;

}

 public void setCountries(String[] countries) {
            this.countries = countries;
            
        }
        
 public List<SelectOption> getMyPickList()
{   
    List<SelectOption> options = new List<SelectOption>();
    List<User> usr= [ Select User.Country__c From  User];

    for(User a : usr){
        options.add(new SelectOption(??,??);
    }

    return options;
}

}

 

Visualforce page

<apex:page sidebar="false" showHeader="false" standardController="User" extensions="MyMSLController">
<apex:pageBlock ><apex:pageBlockSection ><apex:form >
<apex:outputLabel >Name:    </apex:outputLabel>
<apex:outputText value="{!$User.FirstName}"></apex:outputText><br/>
<apex:outputLabel >Select Countries:    </apex:outputLabel>
<apex:selectList value="{!countries}" multiselect="true" size="2" title="Select">
<apex:selectOptions value="{!myPickList}"></apex:selectOptions>
 </apex:selectList>
</apex:form>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>

 

 

Please suggest solution.

Jia HuJia Hu
try this,

List<User> usr= [ Select Lastname, Country__c From User where Country__c != null];

for(User a : usr){
options.add(new SelectOption(a.Lastname, a.Country__c));
}
HemanthrulezHemanthrulez

Thanks Jia Hu,

 

 

I am getting a Null Pointer Exception if I use a.Country__c and moreover, I dont want LastName to be fetched. What should be the item Value in that case.

 

 

 

Visualforce Error


System.NullPointerException: Argument 1 cannot be null
Class.MyMSLController.getMyPickList: line 26, column 1

 

 

 

Jia HuJia Hu
try to use,

options.add(new SelectOption(a.Country__c, a.Country__c));

it should be fine.