You need to sign in to do that
Don't have an account?
SFmaverick
GetList based on Controller Variable
I'm generating a list of an SObject called Shift. The list is being pulled based on the date field. If I use the standard Today(), It correctly shows me the list of records for today.
My page takes user input as text and translates it to a Date called DayDate. I'd like to pull this list based on DayDate instead of Today().
Here's my Page code with the relevant parts highlighted:
<apex:page showHeader="false" standardStyleSheets="false" controller="ScheduleViewController">
<apex:form >
<apex:inputText value="{!BegDate}"/>
<!-- DayDate comes out correctly after inputting a date! -->
<apex:outputText value="{!DayDate}"/>
</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>
My Controller Code:
public class ScheduleViewController {
//Initializes and provides a get method for the user inputted date
public String BegDate { get; set; }
public Date DayDate;
// Turns the User's string input into a date
public Date getDayDate(){
if (BegDate==null) {
return null;
} else {
return stringToDate(BegDate);
}
}
//Gets TheList - an array of records from Shift__c where the Date equals DayDate (it works if I use Today(),
//but not DayDate, even when I verify that DayDate has the same value as Today()!
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 = :DayDate];
System.Debug('MY LIST:' + TheList);
return TheList;
}
//Converts a string from mm/dd/yy to a date
public Date stringToDate(String s){
//Input Date String is in the format mm/dd/yyyy
if(s.length()== 0)
{
return NULL;
}
else{
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);
}
}
}
If its a race condition, you could slightly change your query to ensure that it pulls the latest DayDate prior to executing:
That will force DayDate to be determined from the supplied BegDate prior to the query running.
All Answers
Unfortunately you aren't storing the date that you convert from a string into the DayDate property. Thus your getter will display the converted date correctly, but DayDate when used in the SOQL query will be null.
Changing your getter as follows should help:
Thanks Bob, I actually realized this before you posted and tried it, but it's still not passing the value of DayDate to TheList.
Here's what I have now:
For the Controller---
For the Page ---
Here's the debug log:
The problem is it's not executing the getDayDate until after TheList is already constructed; I don't understand why because that's not logical if you follow the order of code; Would it be possible to create a button that when pressed will re-render the list and refresh the table?
I should add that I entered 9/2/10 as the date for BegDate.
If its a race condition, you could slightly change your query to ensure that it pulls the latest DayDate prior to executing:
That will force DayDate to be determined from the supplied BegDate prior to the query running.
Very nice, that did indeed make the list start pulling the correct DayDate :)
However that puts me at a loss for how I would go about what was next. The next thing I wanted to do was regenerate that list 7 times, each time incrementing DayDate by 1 more than the last generation of the list. I'll be be outputting the list after each generation. Able to offer any help there?
When you say regenerating, is that having 7 different lists on the page or one list that is updated 7 times after an interval?
Well technically it's seven different lists, but the only difference in the call is the filter criteria (which is a date). My only challenge right now is being able to make a call from visualforce that will increment DayDate by one and maintain that value for the next call of the list.
After some debugging, it seems to me that it only runs the getter for my list methods twice, once to initialize and then once when first called. It's not regenerating the list on all the subsequent calls. I've changed my code a bit to have another list generated (called TheList2) and inside this getter I put a call to a method that increments DayDate by one. It works , but even though I call that getter 6 times in my page, it only generates it once and then just reuses it. There a way to fix that? Perhaps with some rerendering? I've never used it so I'm not sure.
Basically this is the call to the second list in my page (this exact code appears 6 times):
And here's the related Methods from my controller:
As you are using HTML tables, I'd suggest that you use repeat tags rather than trying to call getters a number of times.
E.g. in the page:
and then generate everything in one go in the controller:
Yeah thanks, I wondered how to do that with visualforce, I didn't see and consider the repeat option.
I'm trying to figure out a different way to pull the lists, the way I'm doing it and reporting them, it's putting a bullet in a row for each record in the list. This is distorting the output big time.
I tend to use vanilla HTML myself. I find that the standard list components don't give me enough control.
They don't, I want to keep the HTML, I'm thinking I need something other than a DataList to grab the records I need. The DataList is what would be feeding the html the bullets and I want to cut that out but I see no mention of the bullets anywhere. I've looked through all the Salesforce help things, and googled with every word combination.
Any idea where the bullets are even coming from? There is a bullet for each record. Her'es what it looks like.
It's making it so the columns aren't flush with eachother and the bullets are just completely unexpected and unnecessary.
Datalist generates HTML order or unordered lists. That means that each entry in the list is either preceeded by bullets (or discs/squares etc) or numbers. Code such as the following:
generates HTML similar to:
for each element in the list.
If you just want to iterate the list and output some information for each element, simply use a repeat tag, e.g.