4

I have a complicated problem so i'm hoping to ask this question generically:

I see a lot of examples that mostly work just fine with SelectList and SelectOptions.

My particular problem is that i can not get the SelectOptions dropdown menu to display the value i want as the "selected" (or saved) value upon the page rendering.

For example, i get a record from the db with "Relationship" = "Sister" and i want the dropdown to show "Sister".

My dropdown menu is built from a SOQL query and would have

"Mother"
"Father"
"Sister"
"Brother"
“Spouse”

and shown in the window (size=1)

But when the page renders, the top value ("Mother") is always shown rather than the "Sister" (3rd value in dropdown - list with size=1 window) no matter how i try to set it (i've tried about 4000 variations).

Can anyone show me an example of this working? Am i misunderstanding something? This seems so simple but i've been battling this for days. Thank you!!

OK Here's the controller class code.

I load the page with an applicant ID passed in the URL. I go load that applicant record and display it. The "relationship" value is set from that applicant record.

Please note:

  1. When i build the dropdown list i do not hardcode the values for the dropdown menu like all the examples out there. I go to my database and pull them out and dynamically build the list from MyDBList__c entries.

    I think this is the source of the problem something screwy about this because if i hardcode them, everything is fine!

  2. The last item on the page displays the "relationship" correctly in the outputText. The Dropdown, however, does not.

  3. I have all the extra containers on the page to mimic what i have in my system...I left them so i know none of those are the problem.

  4. applicant.relationship__c is a lookup field to the Name in MyDBList__c (from which the dropdown was built).


Controller

public class TVTestControllerClass
{
   public string selectedValue {get;set;}
   public Applicant__c applicant;
   public ID applicantID {get; set;}

   //call ?applicantID=a0N11000004wsh3 to set my applicant
   public TVTestControllerClass(){
     applicantID   = ApexPages.currentPage().getParameters().get('ApplicantID'); 
   }


   //the values for the dropdown come from here
   public SelectOption[] getselectValues() {     
         SelectOption[] options = new SelectOption[]{};
         for(MyDBList__c oneRelationship : [SELECT Id, Name FROM MyDBList__c]) {
             options.add(new SelectOption(
                 String.valueOf(oneRelationship.Id),oneRelationship.Name));                 
         } 
         return options;           
    } 

    public Applicant__c getApplicant() {
        //selectedValue = 'Brother';//default as anything so i know didn't work
        //CORRECTION: This should have been something equiv to this...
        selectedValue = 'a0H1100000B3bFfEAJ';
        applicant = 
               [SELECT Id, Name, //Name is type Text[80]
                   applicantFirstName__c, 
                   relationship__c,
                   relationship__r.Name
                FROM Applicant__c
                WHERE Id = :applicantID];

        //selectedValue = applicant.relationship__r.Name;
        //CORRECTION: This SHOULD have been
        selectedValue = String.valueOf(applicant.relationship__c);


        return applicant;
    }//end getApplicant


}//end TVTestControllerClass

Page

<apex:page controller="TVTestControllerClass" sidebar="false"  showHeader="false">
 <apex:form id="applicantForm" >
 <apex:pageBlock >

 <apex:outputPanel styleClass="panelWrapper" layout="block">

 <apex:outputPanel id="ApplicantDetailPanel">
    <apex:pageblocksection id="ApplicantDetail" columns="1" > 

         <apex:pageBlockSectionItem>
            <apex:outputLabel value="First Name" />
            <apex:inputField value="{!applicant.applicantFirstName__c}"/>
         </apex:pageBlockSectionItem >  

         <apex:pageBlockSectionItem id="ApplRelBlock">
            <apex:outputLabel value="Relationship"/>
                <apex:outputPanel layout="block"> 
                    <apex:actionRegion >
                        <apex:selectList value="{!selectedvalue}" size="1" multiselect="false">
                           <apex:selectOptions value="{!selectValues}"/>
                        </apex:selectList>
                        <apex:actionSupport event="onchange" reRender="testingApplR2VID"/>
                    </apex:actionRegion>
                </apex:outputPanel>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem id="testingApplR2VID">
            <apex:outputLabel value="selectedValue="/>
            <apex:outputText value="{!selectedValue}"/>
        </apex:pageBlockSectionItem>
    </apex:pageblocksection>
  </apex:outputPanel>
  </apex:outputPanel>
  </apex:pageBlock>
  </apex:form>
</apex:page>
  • Can you please paste your code? Also are you using immediate true anywhere? – d_k Jul 14 '16 at 2:19
  • The code is complicated and would take quite a bit of time to pull out the bits but i'll try to do that today. I may be using immediate somewhere. What does that en-danger? Thank you! – Cindy Jul 14 '16 at 14:58
  • Its pretty simple. You are setting the selectOption VALUE to an ID yet setting the selectedValue to an STRING Relationship__r.name. Brother DOES NOT equal a valid ID – Eric Jul 15 '16 at 2:27
  • Are you saying both parameters have to be the same? I'm setting the 1st parameter (building the options list) to a String representation of the ID and the 2nd parameter to the String. If it were hardcoded, it would be: – Cindy Jul 15 '16 at 15:03
  • public SelectOption[] getselectValues(){ return New SelectOption[]{ New SelectOption('a0H1100000B3bG9EAJ','Mother'), New SelectOption('a0H1100000InOUPEA3','Spouse'), New SelectOption('a0H1100000B3bG9EAJ','Sister'), New SelectOption('a0H1100000B3bFzEAJ','Brother') }; } – Cindy Jul 15 '16 at 15:04
10

You need to set the value that the select list is bound to..

public class mytest{
    public string selectedValue {get;set;}

   public class mytest(){
     selectedValue = 'Sister';
   }

   public SelectOption[] getselectValues(){
      return New SelectOption[]{
            New SelectOption('Mother','Mother'),
            New SelectOption('Sister','Sister'),
            New SelectOption('Father','Father')

      }
   }
}

Page

<apex:selectList value="{!selectedvalue}" size="1" multiselect="false">
   <apex:selectOptions value="{!selectValues}"/>
</apex:selectList>

Note in the constructor I set the value of selectedValue to the value I want the select list to be set to.

Key: That bound value must be set to the VALUE and not the LABEL of the Select Option.

For example:

if the select option was SelectOption('A','Sister') then selectedValue needs to be set to A and not Sister.

  • Yes, thank you. These examples are scattered throughout the community and i appreciate your beautifully precise one here. I'm having a deeper problem, somewhere, and i don't know where to look. So, i was pfishing for some knowledge, experience about those weird gotcha(s) that don't come out in these little examples...perhaps a refresh timing or an 'immediate' or ... ? I have no idea what it could be. – Cindy Jul 14 '16 at 15:01
  • @cindy There are none except not setting the value or have problematic code that resets it in another getter. Its pretty basic as far as loading goes. – Eric Jul 14 '16 at 15:03
  • One thing i'm confused about is that some of the examples use and ID for the first parameter in SelectOption (IDofSister, 'Sister')...which i do. I changed them to Strings (because that is the definition of the method) all over but that didn't help. – Cindy Jul 14 '16 at 15:04
  • @Cindy - Start with what was provided in this answer, then build out for there, testing along the way. When you get stuck then you can ask a new question. The first parameter must be a string. Please accept an answer. If you cannot then this question may be closed as too broad. – Eric Jul 14 '16 at 15:52
  • I'm going to do my testing now with my own code and pull out what isn't working for me. I feel like it is way too early to accept an answer because we haven't even touched upon the problem yet, don't you? – Cindy Jul 14 '16 at 23:29
0

You should set the value of selectedValue in your class constructor, and set the value to be relationship__c instead of relationship__r.Name, since your picklist is using the Ids as values.

public class TVTestControllerClass
{
   public string selectedValue {get;set;} 
   public Applicant__c applicant {get; set;}
   public ID applicantID {get; set;}

   //call ?applicantID=a0N11000004wsh3 to set my applicant
   public TVTestControllerClass(){
     applicantID   = ApexPages.currentPage().getParameters().get('ApplicantID'); 

        applicant = 
               [SELECT Id, Name, //Name is type Text[80]
                   applicantFirstName__c, 
                   relationship__c,
                   relationship__r.Name
                FROM Applicant__c
                WHERE Id = :applicantID];

        selectedValue = applicant.relationship__c;
   }


   //the values for the dropdown come from here
   public SelectOption[] getselectValues() { 
         SelectOption[] options = new SelectOption[]{};
         for(MyDBList__c oneRelationship : [SELECT Id, Name FROM MyDBList__c]) {
             options.add(new SelectOption(
                 String.valueOf(oneRelationship.Id),oneRelationship.Name));                 
         } 
         return options;           
    } 


}//end TVTestControllerClass
  • Perhaps i have something backwards but the oneRelationship.Name is the "Label" on the option list. The "Value" is the String.valueOf(...the ID of that label). One of these IDs (converted to string) is the same ID that the applicant.relationship__c holds. So when i load the applicant, i set selectedValue to that String.valueOf that ID (which is in the list). – Cindy Jul 15 '16 at 14:56
  • Have you tried replacing your controller with what I posted as my answer here? What was the result? – martin Jul 15 '16 at 19:45
  • Yes. That works fine. Even when i change the code to reflect what i've edited in the original question (changes marked with "CORRECTION"), it all works fine. But in my production system, I will print out the values and they are all correct. But the dropdown refuses to update even though everything looks perfect. – Cindy Jul 15 '16 at 20:52
  • I can set the value initially (at the start of my getApplicant() and it sets the dropdown just fine. But later when i assign it again, it ignores it. This sounds really lame but could there be anything setting a limit on this process? (like i said, i can print out the values on the screen and they are perfect. Dropdown is the problem not the value.) – Cindy Jul 15 '16 at 21:01
  • Have you tried copying and pasting my answer? There are more differences than just what you corrected. – martin Jul 15 '16 at 21:08
0

Ok...after a LOT of time...it turns out that the problem was an immediate="true" on the button i used to edit the applicant which called getApplicant() and set the "selectedValue" variable to be displayed on the page.

[ie the line selectedValue = 'a0H1100000B3bFfEAJ'; set just fine. but the later line, selectedValue = String.valueOf(applicant.relationship__c); did not until i took out the immediate=true in the page. ]

Obviously, I"m not familiar enough with immediate setting but the result was that some of the page (all other values on the page -- which included some complicated stuff) was perfect but the dropdown menu would not update in time for the drawing of the page.

THANK YOU GUYS!!!

  • Immediate=true should have no effect during page render so I am a bit confused by your question and answer. – Eric Jul 16 '16 at 15:12
  • I've gotten used to strange, unrelated things wreaking havoc in Visualforce. But it was undeniably the problem. Everything else on the page rendered correctly except the Dropdown (including OutputText showing the value that should have been set in the dropdown. So, the value was set correctly). And when immediate=true was taken out of the Command button (action) that loaded the applicant onto the page (with the relationship field), everything set fine including the Dropdown. – Cindy Jul 16 '16 at 18:55
  • maybe it is terminology then. Render of the page it when it loads. If you are talking about the events after you click a command button that is different. I also do not see a command button on your page. Asking a question with the correct details will get you better answers in the future and not have the people here focusing on a "Page Rendering" issue when it is really an issues related to actions resulting from the execution of an ajax action. Also, no-one could have found that in your question as there is no command button is your examples anywhere that I can see. Glad you got it fixed – Eric Jul 16 '16 at 19:03
  • That's why i asked the original question the way i did...asking for known "gotchas" because my problem was too complicated to give it all in the question. Then you guys asked for code so i added it....but i knew it was a distraction because in a vacuum, these things almost always work. Its the crazy little unknown things that always make a mess. Anyway, that's the story. Thanks so much for your help! – Cindy Jul 16 '16 at 22:38
  • Your question was gotchas on "Page render" which is why it was tough to focus in on "immediate" attribute which only comes into play after you click a button (not on page render). The more specific your details the quicker you get detailed and appropriate responses. It is tough to see your screen / actions through the forums. We ultimately got there so please take this just as it is typed. You did nothing wrong and you got it solved. Just food for thought on how to ask a good question really. Forum is not suited for "broad questions" either. We are all here to help! – Eric Jul 16 '16 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.