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
FastSnailFastSnail 

Issue with transfering PickList selection to Select statment

Hello everyone,

I am trying here to use the result of a Picklist value to get a list of related object. Although the Selected value is visible cleary in ViewState, it is not actuated in the related object Select statment. Why?

Thanks in advance for your help

Jerome

Controller

public with sharing class CNModConMoyCont {
public String MarqueSelected { public get; public set; }
public list<Organism__c> marqueList=[select Id, Name from Organism__c];
public list<Organism__c> getMarqueList(){return marqueList;}
public void setMarqueList(list<Organism__c> data) { marqueList=data; }
public List<SelectOption> marqueSelOpt;
public List<SelectOption> getMarqueSelOpt() {
List<SelectOption> options = new List<SelectOption>();
for (Organism__c Org : marqueList) {
options.add(new SelectOption( Org.Id, Org.Name));
}
return options;
}
public void setMarqueSelOpt(List<SelectOption> data) {marqueSelOpt=data;}
public list<Model__c> modelList { public get; public set; }
public CNModConMoyCont() {
modelList = [select Id, Name, Lien_Marque__c from Model__c where Lien_Marque__c =: MarqueSelected]; //always null
}
}

Page

<apex:page controller="CNModConMoyCont" showHeader="false" sidebar="false" id="CNModConMoy" >
<apex:form >
<apex:pageBlock >
<apex:selectList value="{!MarqueSelected}" multiselect="false" >
<apex:selectOptions value="{!marqueSelOpt}" />
</apex:selectList>
<apex:commandLink value="pick" rerender="datalist, modellist" />
<apex:outputPanel id="datalist" ><br/>
{!MarqueSelected}
</apex:outputPanel>
<apex:dataTable value="{!ModelList}" var="mod" id="modellist">
<apex:column >
<apex:outputText value="{!mod.Name}" />
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
Karthikeyan JayabalKarthikeyan Jayabal

Since the SELECT statement is within the constructor, it will be called only when the page initializes. So, during page initialization the select value will be null.

Just add another action method with this query & call it using the commandLink action, then it will work as you expect.

 

Sample code:

Page:

<apex:page controller="controller_selectListDemo">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Refresh" action="{!queryContacts}" status="cStatus" reRender="cTable"/>
                <apex:actionStatus id="cStatus" startText="Querying Contacts..."/>
            </apex:pageBlockButtons>
            <table width="100%">
                <tr>
                    <td width="20%">
                        <apex:selectList value="{!selectedValue}" multiselect="false" size="1">
                            <apex:selectOptions value="{!LeadSources}"/>
                        </apex:selectList>
                    </td>
                    <td width="80%">
                        <apex:pageBlockTable id="cTable" value="{!contactResults}" var="c">
                            <apex:column value="{!c.Name}"/>
                            <apex:column value="{!c.LeadSource}"/>
                        </apex:pageBlockTable>
                    </td>
                </tr>
            </table>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 Controller:

public class controller_selectListDemo {
    public String selectedValue { get; set; }
    public List<Contact> contactResults  { get; set; }
    
    public List<SelectOption> getLeadSources() {
        List<SelectOption> options = new List<SelectOption>();
        List<Schema.PicklistEntry> cplEntries = Schema.sObjectType.Contact.fields.LeadSource.getPicklistValues();
        for(Schema.PicklistEntry p : cplEntries)
            options.add(new SelectOption(p.getLabel(),p.getLabel()));
        return options;
    }
    
    public PageReference queryContacts() {
        contactResults = [Select Id, Name, LeadSource from Contact Where LeadSource = :selectedValue];
        return null;
    }
}

 

 

All Answers

Karthikeyan JayabalKarthikeyan Jayabal

Since the SELECT statement is within the constructor, it will be called only when the page initializes. So, during page initialization the select value will be null.

Just add another action method with this query & call it using the commandLink action, then it will work as you expect.

 

Sample code:

Page:

<apex:page controller="controller_selectListDemo">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Refresh" action="{!queryContacts}" status="cStatus" reRender="cTable"/>
                <apex:actionStatus id="cStatus" startText="Querying Contacts..."/>
            </apex:pageBlockButtons>
            <table width="100%">
                <tr>
                    <td width="20%">
                        <apex:selectList value="{!selectedValue}" multiselect="false" size="1">
                            <apex:selectOptions value="{!LeadSources}"/>
                        </apex:selectList>
                    </td>
                    <td width="80%">
                        <apex:pageBlockTable id="cTable" value="{!contactResults}" var="c">
                            <apex:column value="{!c.Name}"/>
                            <apex:column value="{!c.LeadSource}"/>
                        </apex:pageBlockTable>
                    </td>
                </tr>
            </table>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 Controller:

public class controller_selectListDemo {
    public String selectedValue { get; set; }
    public List<Contact> contactResults  { get; set; }
    
    public List<SelectOption> getLeadSources() {
        List<SelectOption> options = new List<SelectOption>();
        List<Schema.PicklistEntry> cplEntries = Schema.sObjectType.Contact.fields.LeadSource.getPicklistValues();
        for(Schema.PicklistEntry p : cplEntries)
            options.add(new SelectOption(p.getLabel(),p.getLabel()));
        return options;
    }
    
    public PageReference queryContacts() {
        contactResults = [Select Id, Name, LeadSource from Contact Where LeadSource = :selectedValue];
        return null;
    }
}

 

 

This was selected as the best answer
FastSnailFastSnail

Thank you very much. This was it.Simple to you but not obvious to me. I am all set now.

Thanks again for your help.

Jerome