+ Start a Discussion
SFmaverickSFmaverick 

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);
}
}
}

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

If its a race condition, you could slightly change your query to ensure that it pulls the latest DayDate prior to executing:

 

 

  Date theDate=getDayDate();

  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 = :theDate];

 

That will force DayDate to be determined from the supplied BegDate prior to the query running.

 

All Answers

bob_buzzardbob_buzzard

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:

 

 

  public Date getDayDate(){
        if (BegDate==null) {
            return null;
        } else {
            DayDate= stringToDate(BegDate);
            return DayDate;
        }
    }

 

 

SFmaverickSFmaverick

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---

 

 

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 {
DayDate = stringToDate(BegDate);
System.Debug('DayDate' + DayDate);
return DayDate;
}
}

//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);
}
}

//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 = :DayDate];
System.Debug('MY LIST:' + TheList);
System.Debug('DayDate:' + DayDate);

return TheList;
}
}

 

 

For the Page ---

 

 

<apex:page showHeader="false" standardStyleSheets="false" controller="ScheduleViewController">
  <apex:form >
    <apex:inputText value="{!BegDate}"/>
    <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>

 

 

Here's the debug log:

 

 

19.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
10:18:7.402|EXECUTION_STARTED
10:18:7.403|CODE_UNIT_STARTED|[EXTERNAL]|066T00000008n8x|VF: /apex/scheduleview
10:18:7.403|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000005BO9|ScheduleViewController get(TheList)
10:18:7.403|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000005BO9|ScheduleViewController invoke(getTheList)
10:18:7.403|SOQL_EXECUTE_BEGIN|[35,34]|Aggregations:0|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
10:18:7.420|SOQL_EXECUTE_END|[35,34]|Rows:0
10:18:7.421|METHOD_ENTRY|[36,9]|System.Debug(String)
10:18:7.421|USER_DEBUG|[36,9]|DEBUG|MY LIST:()
10:18:7.421|METHOD_EXIT|[36,9]|System.Debug(String)
10:18:7.421|METHOD_ENTRY|[37,9]|System.Debug(String)
10:18:7.421|USER_DEBUG|[37,9]|DEBUG|DayDate:null
10:18:7.421|METHOD_EXIT|[37,9]|System.Debug(String)
10:18:7.421|CODE_UNIT_FINISHED|ScheduleViewController invoke(getTheList)
10:18:7.421|CODE_UNIT_FINISHED|ScheduleViewController get(TheList)
10:18:7.422|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000005BO9|ScheduleViewController get(BegDate)
10:18:7.422|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000005BO9|BegDate
10:18:7.422|CODE_UNIT_FINISHED|BegDate
10:18:7.422|CODE_UNIT_FINISHED|ScheduleViewController get(BegDate)
10:18:7.423|CODE_UNIT_STARTED|[EXTERNAL]|ScheduleViewController set(BegDate,09/02/2010)
10:18:7.423|CODE_UNIT_STARTED|[EXTERNAL]|ScheduleViewController set(BegDate,09/02/2010)
10:18:7.423|CODE_UNIT_FINISHED|ScheduleViewController set(BegDate,09/02/2010)
10:18:7.423|CODE_UNIT_FINISHED|ScheduleViewController set(BegDate,09/02/2010)
10:18:7.428|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000005BO9|ScheduleViewController get(TheList)
10:18:7.428|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000005BO9|ScheduleViewController invoke(getTheList)
10:18:7.428|SOQL_EXECUTE_BEGIN|[35,34]|Aggregations:0|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
10:18:7.436|SOQL_EXECUTE_END|[35,34]|Rows:0
10:18:7.436|METHOD_ENTRY|[36,9]|System.Debug(String)
10:18:7.436|USER_DEBUG|[36,9]|DEBUG|MY LIST:()
10:18:7.436|METHOD_EXIT|[36,9]|System.Debug(String)
10:18:7.436|METHOD_ENTRY|[37,9]|System.Debug(String)
10:18:7.436|USER_DEBUG|[37,9]|DEBUG|DayDate:null
10:18:7.436|METHOD_EXIT|[37,9]|System.Debug(String)
10:18:7.436|CODE_UNIT_FINISHED|ScheduleViewController invoke(getTheList)
10:18:7.436|CODE_UNIT_FINISHED|ScheduleViewController get(TheList)
10:18:7.438|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000005BO9|ScheduleViewController get(BegDate)
10:18:7.438|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000005BO9|BegDate
10:18:7.438|CODE_UNIT_FINISHED|BegDate
10:18:7.438|CODE_UNIT_FINISHED|ScheduleViewController get(BegDate)
10:18:7.439|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000005BO9|ScheduleViewController get(DayDate)
10:18:7.439|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000005BO9|ScheduleViewController invoke(getDayDate)
10:18:7.439|METHOD_ENTRY|[11,23]|01pT00000005BO9|ScheduleViewController.stringToDate(String)
10:18:7.439|METHOD_ENTRY|[20,10]|String.length()
10:18:7.439|METHOD_EXIT|[20,10]|String.length()
10:18:7.439|METHOD_ENTRY|[25,29]|String.split(String)
10:18:7.439|METHOD_EXIT|[25,29]|String.split(String)
10:18:7.439|METHOD_ENTRY|[26,20]|Integer.valueOf(String)
10:18:7.439|METHOD_EXIT|[26,20]|Integer.valueOf(String)
10:18:7.439|METHOD_ENTRY|[27,19]|Integer.valueOf(String)
10:18:7.439|METHOD_EXIT|[27,19]|Integer.valueOf(String)
10:18:7.439|METHOD_ENTRY|[28,19]|Integer.valueOf(String)
10:18:7.439|METHOD_EXIT|[28,19]|Integer.valueOf(String)
10:18:7.440|METHOD_ENTRY|[29,14]|date.newInstance(Integer, Integer, Integer)
10:18:7.440|METHOD_EXIT|[29,14]|date.newInstance(Integer, Integer, Integer)
10:18:7.440|METHOD_EXIT|[11,23]|ScheduleViewController.stringToDate(String)
10:18:7.440|METHOD_ENTRY|[12,13]|System.Debug(String)
10:18:7.440|USER_DEBUG|[12,13]|DEBUG|DayDate2010-09-02 00:00:00
10:18:7.440|METHOD_EXIT|[12,13]|System.Debug(String)
10:18:7.440|CODE_UNIT_FINISHED|ScheduleViewController invoke(getDayDate)
10:18:7.440|CODE_UNIT_FINISHED|ScheduleViewController get(DayDate)
10:18:7.447|CUMULATIVE_LIMIT_USAGE
10:18:7.447|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 0 out of 10000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 100
  Number of DML rows: 0 out of 10000
  Number of script statements: 16 out of 200000
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 10
  Number of record type describes: 0 out of 10
  Number of child relationships describes: 0 out of 10
  Number of picklist describes: 0 out of 10
  Number of future calls: 0 out of 10
  Number of find similar calls: 0 out of 10
  Number of System.runAs() invocations: 0 out of 20

10:18:7.447|CUMULATIVE_LIMIT_USAGE_END

10:18:7.447|CODE_UNIT_FINISHED|VF: /apex/scheduleview
10:18:7.447|EXECUTION_FINISHED

 

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.

 

bob_buzzardbob_buzzard

If its a race condition, you could slightly change your query to ensure that it pulls the latest DayDate prior to executing:

 

 

  Date theDate=getDayDate();

  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 = :theDate];

 

That will force DayDate to be determined from the supplied BegDate prior to the query running.

 

This was selected as the best answer
SFmaverickSFmaverick

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?

bob_buzzardbob_buzzard

When you say regenerating, is that having 7 different lists on the page or one list that is updated 7 times after an interval?

 

 

SFmaverickSFmaverick

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):

 

 

        <td>
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
        <apex:dataList value="{!TheList2}" var="item"> <!-- Gets the list of shifts -->
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}
            <br></br>{!item.Day_of_week__c}</td>
            </tr>
        </apex:dataList>
        </table>
        </td>

 

 

And here's the related Methods from my controller:

 

 

    public List<Shift__c> getTheList2() {
        Date NewDate = IncrementDayDate();
        List<Shift__c> TheList2 = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = :NewDate];
        return TheList2;
    }


    public Date IncrementDayDate(){
        if (DayDate!=null) {
            DayDate = DayDate + 1;
        }
        return DayDate;
    }

 

 

bob_buzzardbob_buzzard

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:

 

 

  <td>
      <apex:repeat value="{!theLists}" var="theList>  <!-- Gets the list of list of shifts -->
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
        <apex:dataList value="{!theList}" var="item">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}
            <br></br>{!item.Day_of_week__c}</td>
            </tr>
        </apex:dataList>
        </table>
      </apex:repeat>
        </td>

 

 

and then generate everything in one go in the controller:

 

 

 public List<Shift__c> getTheList2() {
        Date NewDate = IncrementDayDate();
        List<Shift__c> TheList2 = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = :NewDate];
        return TheList2;
    }


    public Date IncrementDayDate(){
        if (DayDate!=null) {
            DayDate = DayDate + 1;
        }
        return DayDate;
    }

 public List<List<Shift__c>> getTheLists()
 {
    List<List<Shift__c>> result=new List<List<Shift__c>>();
    for (Integer i=0; i<7; i++)
    {
       result.add(getTheList2);
    }

    return result;
 }

 

 

 

SFmaverickSFmaverick

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.

bob_buzzardbob_buzzard

I tend to use vanilla HTML myself.  I find that the standard list components don't give me enough control.

SFmaverickSFmaverick

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.

bob_buzzardbob_buzzard

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:

 

 

<apex:datalist var="{!items}" value="item">
   <apex:outputfield value="{!item.name}"/>
</apex:datalist>

 

 

generates HTML similar to:

 

 

<ul>
  <li>ItemName</li>
</ul>

 

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.

 

 

<apex:repeat value="{!items"} var="item">
  <apex:outputfield var="{!item.Name}"/>
  <br/>
</apex:repeat>