2

i want to disply picklist value based on query value in my controller. but its display nothing . the code is like:

<apex:page controller="Pre_process2">
<apex:form >
 <apex:pageBlock title="Pre Process Payroll">
    <apex:pageBlockSection columns="2" showHeader="false" >
        <apex:outputLabel value="Period Code" for="accts"></apex:outputLabel> 
        <apex:selectList id="PeriodCode" value="{!PeriodCode}" size="1" title="Period Code"> 
                <apex:selectOptions value="{!PeriodCode}"></apex:selectOptions> 
        </apex:selectList> 
    </apex:pageBlockSection>
 </apex:pageBlock>
</apex:form>

the controller is:

public with sharing class Pre_process2 {
public String PeriodCode {get; set;}

 //public visualforcepage_picklist(ApexPages.StandardController stdController) 

//=============================== get picklist value for Period Code ================================== 
 public List<selectOption> getPeriodCode() 
 {        
    List<selectOption> options = new List<selectOption>(); 

    options.add(new selectOption('', '- None -')); 
    List<aggregateResult> results = [SELECT  Period__r.name FROM HREMPASSIGNMENT__c group by Period__r.name];
    for (AggregateResult ar : results)
    {
        string name = string.valueOf(ar.get('Name')); 
        options.add(new selectOption(name, name)); 
    }
    return options;    
 }}

do i miss something?

thanks and regards

Willy

4
  • 2
    Are you sure you are getting results in your query? Commented Sep 2, 2016 at 5:14
  • 2
    Can you please try with changing string name Periodcode to any other name.
    – Reshma
    Commented Sep 2, 2016 at 5:44
  • @Tushar, yes i got result from my query.
    – Kiunx
    Commented Sep 2, 2016 at 5:47
  • @Kiunx Maybe it's related to the sharing rules on the object ? Commented Sep 2, 2016 at 7:36

2 Answers 2

3

You have a string variable PeriodCode and a method getPeriodCode of return type List<SelectOption>. This is confusing and not a good practice.

I just changed the String variable name and it worked for me. Here is what I changed.

Class:public String PeriCodeStr {get;set;}

VF: <apex:selectList id="PeriodCode" value="{!PeriodCodeStr}" size="1" title="Period Code">

PS: Experts in this community can explain better this behavior, I do not fully understand this. According to my understanding we have two same get methods with different return types. So either this should not allowed to be saved and compiled. If it is saved then it should work properly??

0

In your loop change this

string name = string.valueOf(ar.get('Name')); 
options.add(new selectOption(name, name)); 

To

Id id = (Id) ar.get('id'); 
string name = string.valueOf(ar.get('Name'));
options.add(new selectOption(id, name)); 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .