0

I'm new to VisualForce and trying to figure out how to pass the values a user selected from a birthday picklist into my controller. The picklists are fields of my Contact object and one is dependent on the other (the month selected determines the days selectable). Whenever I try to submit a value, nothing shows up (I have at least one contact for every birthday). How do I do this correctly?

Here is my VisualForce code:

<apex:page controller="BirthdayController">
    <apex:form >
        <h3>Enter Your Birthday:</h3>
        Month: <apex:inputField value="{!c.Month__c}"/>
        Day: <apex:inputField value="{!c.Day__c}"/>
        <apex:commandButton value="Submit" action="{!submit}"/>
        <apex:pageBlock title="Birthdays">
            <apex:pageBlockTable value="{!Birthdays}" var="b">
                <apex:column value="{!b.FirstName}"/>
                <apex:column value="{!b.LastName}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

And here's my controller code:

public class BirthdayController
{
    public Contact c {get; set;}
    public String bday;
    public void submit()
    {
        bday = c.Month__c + '/' + c.Day__c;
    }
    public List<Contact> getBirthdays()
    {
        List<Contact> results = [SELECT FirstName, LastName FROM Contact WHERE Birthday__C = :bday];
        return results;
    }
}

EDIT: Basically what I'm trying to do is make it so that a user can select a date using the picklists and have all the contacts with matching birthdays show up. I know I could do this by manually creating a populating a selection list, but I want to use the contact fields because they are dependent. Is what I'm trying to do possible?

0

You need to create the Contact object whose fields your page sets, for example in your controller's constructor:

public BirthdayController() {
    c = new Contact();
}
  • That stopped the null issue but I guess I didn't explain what I'm trying to do. I want a user to be able to select a date from the picklist so that all contacts with their birthday as that date will be listed. So I'm not working with any one specific contact. Am I doing this completely wrong? – user25888 Oct 28 '15 at 16:11
  • @user25888 The pattern you are using - using an object to present fields to use to filter other data - makes sense. The getBirthdays call in submit won't do anything because it will be the Visualforce referencing getBirthdays that will return the data. Not sure why you are not seeing results through. – Keith C Oct 28 '15 at 16:34
  • Thanks, I removed that and realized that my problem was in how I defined my birthday field. I'm an idiot. – user25888 Oct 28 '15 at 17:25

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.