+ Start a Discussion
SFmaverickSFmaverick 

getList based on user input

Currently my controller looks like this:

 

 

public class ScheduleViewController {
    public List<Shift__c> getTheList() {
        List<Shift__c> TheList = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c FROM Shift__c WHERE Date__c = TODAY];
        System.Debug('MY LIST:' + TheList);
        return TheList;
    }
}

 

The list is filtered by the date, where the Date is equal to today. Instead, I'd like the user to be able to specify a date and have the list pulled from that date. I'm not sure how I can pass the date as a parameter to the controller method.\

 

My current call to the list is simply:

 

 

    <apex:dataList value="{!TheList}" var="item">

 

How would I go about querying that list based on a user specified date?

 

 

bob_buzzardbob_buzzard

I'd suggest the following, though there are no doubt other ways to implement this:

 

(1) Create an input element on your VF page that captures the date - which one of these you choose depends on how you are storing the date - e.g. an sobject field, a text field that is converted to a date etc.

(2) Set the value of the input element to a property from your controller.

(3) Create an apex:actionfunction that invokes a method on your controller to rebuild "thelist" based on the property value.

(4) Add a rerender attribute to your actionfunction so that it just redraws the datalist

(5) Add an onchange event to your input element that calls the actionfunction

Pradeep_NavatarPradeep_Navatar

You can use <apex:param> component along with <apex:datalist> to pass date as a parameter.

SFmaverickSFmaverick

I'm unfamiliar with how to use <apex:param>, maybe you can help me out? Here's the current code from my page / controller. I'm currently getting this error:

 

 

System.NullPointerException: Attempt to de-reference a null object

Class.ScheduleViewController.stringToDate: line 21, column 29 Class.ScheduleViewController: line 6, column 27 Class.ScheduleViewController: line 1, column 14

 

 

My Page code is:

 

 

<apex:page showHeader="false" standardStyleSheets="false" controller="ScheduleViewController">
  <apex:form>
    <apex:inputText value="{!BegDate}"/> 
  </apex:form>
  
  <apex:outputPanel >
    <apex:dataList value="{!TheList}" var="item"> <!-- Gets the list of shifts -->
    <table border="0" bordcolor="00000" cellspacing="0" width="80%" bgcolor ="#000000"> <!-- Table that contains 7 day of weektables -->
        <tr>
        <td>
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}</td>
            </tr>
        </table>
        </td>
        
        <td>
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}</td>
            </tr>
        </table>
        </td>
        
        <td>
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}</td>
            </tr>
        </table>
        </td>
        
        <td>
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}</td>
            </tr>
        </table>
        </td>
        
        <td>
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}</td>
            </tr>
        </table>
        </td>
        
        <td>
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}</td>
            </tr>
        </table>
        </td>
        
        <td>
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}</td>
            </tr>
        </table>
        </td>
        </tr>
    </table>
    </apex:dataList>
  </apex:outputPanel>
</apex:page>

 

 

 

and my controller code is (with the lines giving the error highlighted in red:

 

 

public class ScheduleViewController {
//Initializes and provides a get method for the user inputted date
public String BegDate { get; set; }

//Initalizes a date field to put the string into for use as a filter criteria
Public Date DayDate = stringToDate(BegDate);

//Adds one day to the current DayDate field in order to provide consecutive days' lists
public void setDayDate()
{
DayDate = DayDate + 1;
}

//Gets TheList - an array of records from Shift__c where the Date equals DayDate
public List<Shift__c> getTheList() {
List<Shift__c> TheList = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = TODAY];
System.Debug('MY LIST:' + TheList);
return TheList;
}

//Converts a string from mm/dd/yyyy to a date
public Date stringToDate(String s){
//Input Date String is in the format mm/dd/yyyy
String[] stringDate = s.split('/');
Integer m = Integer.valueOf(stringDate[0]);
Integer d = Integer.valueOf(stringDate[1]);
Integer y = Integer.valueOf(stringDate[2]);
return date.newInstance(y,m,d);
}
}

 

 

 

Appreciate all the help!

 

bob_buzzardbob_buzzard

I'm sure this got resolved, but for anyone coming to this thread, the stringtodate error was caused by the following lines:

 

//Initializes and provides a get method for the user inputted date
    public String BegDate { get; set; }

    //Initalizes a date field to put the string into for use as a filter criteria
    Public Date DayDate = stringToDate(BegDate);

 as BegDate is not initialized, only declared, it will have a value of null.  In the stringToDate function, this value is assumed not to be null:

 

   //Input Date String is in the format mm/dd/yyyy
      String[] stringDate = s.split('/');

 the solution is either to null protect the stringToDate function, or move the initialisation of DayDate to the constructor, and ensuring that BegDate has a value.