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
doudou 

Dropdown list with a soql query

Hello !
I'm trying to create a dropdown based on a soql query : 
public without sharing class ExtranetV2_TransfertsCommController2 {
//some code

public List<Commande__c> creneauHoraire = new List<Commande__c>();
    public List<SelectOption> horaire {
    	get{
    		creneauHoraire = [select Cr_neau_horaire_enl_vement__c from Commande__c];
    		horaire = new List<SelectOption>();
    		for(Commande__c ch : creneauHoraire){
    			horaire.add(new SelectOption(ch.Cr_neau_horaire_enl_vement__c));
    		}
    		return horaire;
    	}
    	set;
    }

public ExtranetV2_TransfertsCommController2(){
//some code

}

}
here i call it in my visualforce page :
 
<apex:pageBlock>
                                    <apex:form>
                                        <apex:selectList size="1">
                                            <label>Créneau horaire </label><apex:selectOptions value="{!horaire}"></apex:selectOptions>
                                        </apex:selectList>
                                    </apex:form>
                                    </apex:pageBlock>

my problem is that i get an error in my apex class : Constructor not defined: [System.SelectOption].<Constructor>(String)
and i don't understand how fix that error and make my dropdown working...

Thank you for your help ;)
Best Answer chosen by dou
Vivek DeshmaneVivek Deshmane
You can try this and let me know.
public without sharing class ExtranetV2_TransfertsCommController2 {
//some code

public List<Commande__c> creneauHoraire = new List<Commande__c>();
    public List<SelectOption> horaire {
 
		get{
   	List<SelectOption> Temphoraire =new List<SelectOption>();
    		creneauHoraire = [select Cr_neau_horaire_enl_vement__c from Commande__c];
    		if(creneauHoraire !=null && !creneauHoraire.isEmpty())
			{
    		for(Commande__c ch : creneauHoraire){
			    if(ch.Cr_neau_horaire_enl_vement__c !=null && String.isNotEmpty(ch.Cr_neau_horaire_enl_vement__c))
    			Temphoraire.add(new SelectOption(ch.Cr_neau_horaire_enl_vement__c,ch.Cr_neau_horaire_enl_vement__c));
    		}
		}
    		return Temphoraire;
    	}
    	set;
    }



}

 

All Answers

Vivek DeshmaneVivek Deshmane
Hi,
You can try below code and let me know if it works.
public without sharing class ExtranetV2_TransfertsCommController2 {
//some code

public List<Commande__c> creneauHoraire = new List<Commande__c>();
    public List<SelectOption> horaire {
    	List<SelectOption> Temphoraire =new List<SelectOption>();
		get{
    		creneauHoraire = [select Cr_neau_horaire_enl_vement__c from Commande__c];
    		if(creneauHoraire !=null && !creneauHoraire.isEmpty())
			{
    		for(Commande__c ch : creneauHoraire){
			    if(ch.Cr_neau_horaire_enl_vement__c !=null && String.isNotEmpty(ch.Cr_neau_horaire_enl_vement__c))
    			Temphoraire.add(new SelectOption(ch.Cr_neau_horaire_enl_vement__c,ch.Cr_neau_horaire_enl_vement__c));
    		}
		}
    		return Temphoraire;
    	}
    	set;
    }



}

Best Regards,
-Vivek
doudou
Thank you for the answer, I try your code but a different error appears : unexpected token: 'List' (at line 6 of your code) and i still don't know how to fix it...
Vivek DeshmaneVivek Deshmane
You can try this and let me know.
public without sharing class ExtranetV2_TransfertsCommController2 {
//some code

public List<Commande__c> creneauHoraire = new List<Commande__c>();
    public List<SelectOption> horaire {
 
		get{
   	List<SelectOption> Temphoraire =new List<SelectOption>();
    		creneauHoraire = [select Cr_neau_horaire_enl_vement__c from Commande__c];
    		if(creneauHoraire !=null && !creneauHoraire.isEmpty())
			{
    		for(Commande__c ch : creneauHoraire){
			    if(ch.Cr_neau_horaire_enl_vement__c !=null && String.isNotEmpty(ch.Cr_neau_horaire_enl_vement__c))
    			Temphoraire.add(new SelectOption(ch.Cr_neau_horaire_enl_vement__c,ch.Cr_neau_horaire_enl_vement__c));
    		}
		}
    		return Temphoraire;
    	}
    	set;
    }



}

 
This was selected as the best answer
doudou
Hi :)
Thank you it's working, and since i have try another method wich works too, I found help here for those who are getting trouble with the same thing : https://developer.salesforce.com/blogs/developer-relations/2008/12/using-the-metadata-api-to-retrieve-picklist-values.html