You need to sign in to do that
Don't have an account?
soumyodeep Guha 6
Issue in passing date parameter to soql in controller class
Hi
i need help in a issue where i am not able to fetch records based on a date parameter passed to a soql query in a controller class for a VF page.
The issue is with the line 10 in the VF page code where it takes date as an input, and passes it to the controller class and used in the soql in line 14, which return no records even though its there. If i hardcode any date in the soql query as in line 15 then records are returned.
can anybody help me to correct this as i am not getting any error in the code written but it does not seems to work.
VF page
=================================================================
<apex:page sidebar="false" showHeader="false" standardController="Ticket_transaction__c" extensions="MovieController">
<apex:form >
<apex:sectionHeader title="Ticket Booking Page"/>
<apex:pageblock title="Movie Preference">
<apex:pageblockSection title="Movie Language Preference" collapsible="false">
<apex:inputField value="{!Ticket_transaction__c.Language_preference__c}"/>
</apex:pageblockSection>
<apex:pageblockSection title="Movie Date" collapsible="false">
<apex:inputfield value="{!Ticket_transaction__c.Movie_Date__c}" id="MovieDate"/>
<br/><br/>
<apex:commandButton value="Display Movies.." action="{!MovieList}"/>
<br/><br/>
<apex:pageBlockTable value="{!Mlist}" var="M">
<apex:column value="{!M.Movie__c}"/>
<apex:column value="{!M.Theater__c}"/>
<apex:column value="{!M.Show_TIme__c}"/>
<apex:column value="{!M.Movie_Language__c}"/>
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageblock>
</apex:form>
</apex:page>
===========================================================================
Controller
============================================================================
public class MovieController {
public MovieController(ApexPages.StandardController controller) {
}
public list<Movie_at_Theater__c> Mlist{get;set;}
public string Language{get;set;}
public Date MovieDate{get;set;}
//public string query{get;set;}
public Ticket_transaction__c tc {get;set;}
public void MovieList()
{
if(Language==null)
{
Mlist=database.query('Select Movie__c, Theater__c, Show_TIme__c, Movie_Language__c from Movie_at_Theater__c where Movie_Date__c=:MovieDate');
//Mlist=database.query('Select Movie__c, Theater__c, Show_TIme__c, Movie_Language__c,Movie_Date__c from Movie_at_Theater__c where Movie_Date__c=2016-08-28' );
//query='Select Movie__c, Theater__c, Show_TIme__c, Movie_Language__c,Movie_Date__c from Movie_at_Theater__c';
}
else
{
list<Movie_at_Theater__c>Templist=database.query('Select Movie__c, Theater__c, Show_TIme__c, Movie_Language__c from Movie_at_Theater__c where Movie_Date__c='+MovieDate);
for( Movie_at_Theater__c Ml : Templist)
{
If(Language==Ml.Movie_Language__c)
{
Mlist.add(Ml);
}
Else{}
}
}
}
}
i need help in a issue where i am not able to fetch records based on a date parameter passed to a soql query in a controller class for a VF page.
The issue is with the line 10 in the VF page code where it takes date as an input, and passes it to the controller class and used in the soql in line 14, which return no records even though its there. If i hardcode any date in the soql query as in line 15 then records are returned.
can anybody help me to correct this as i am not getting any error in the code written but it does not seems to work.
VF page
=================================================================
<apex:page sidebar="false" showHeader="false" standardController="Ticket_transaction__c" extensions="MovieController">
<apex:form >
<apex:sectionHeader title="Ticket Booking Page"/>
<apex:pageblock title="Movie Preference">
<apex:pageblockSection title="Movie Language Preference" collapsible="false">
<apex:inputField value="{!Ticket_transaction__c.Language_preference__c}"/>
</apex:pageblockSection>
<apex:pageblockSection title="Movie Date" collapsible="false">
<apex:inputfield value="{!Ticket_transaction__c.Movie_Date__c}" id="MovieDate"/>
<br/><br/>
<apex:commandButton value="Display Movies.." action="{!MovieList}"/>
<br/><br/>
<apex:pageBlockTable value="{!Mlist}" var="M">
<apex:column value="{!M.Movie__c}"/>
<apex:column value="{!M.Theater__c}"/>
<apex:column value="{!M.Show_TIme__c}"/>
<apex:column value="{!M.Movie_Language__c}"/>
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageblock>
</apex:form>
</apex:page>
===========================================================================
Controller
============================================================================
public class MovieController {
public MovieController(ApexPages.StandardController controller) {
}
public list<Movie_at_Theater__c> Mlist{get;set;}
public string Language{get;set;}
public Date MovieDate{get;set;}
//public string query{get;set;}
public Ticket_transaction__c tc {get;set;}
public void MovieList()
{
if(Language==null)
{
Mlist=database.query('Select Movie__c, Theater__c, Show_TIme__c, Movie_Language__c from Movie_at_Theater__c where Movie_Date__c=:MovieDate');
//Mlist=database.query('Select Movie__c, Theater__c, Show_TIme__c, Movie_Language__c,Movie_Date__c from Movie_at_Theater__c where Movie_Date__c=2016-08-28' );
//query='Select Movie__c, Theater__c, Show_TIme__c, Movie_Language__c,Movie_Date__c from Movie_at_Theater__c';
}
else
{
list<Movie_at_Theater__c>Templist=database.query('Select Movie__c, Theater__c, Show_TIme__c, Movie_Language__c from Movie_at_Theater__c where Movie_Date__c='+MovieDate);
for( Movie_at_Theater__c Ml : Templist)
{
If(Language==Ml.Movie_Language__c)
{
Mlist.add(Ml);
}
Else{}
}
}
}
}
Tyr printing the value of MovieDate using
before your query and see what its returning.
Hope this helps you!
Thanks and Regards
Sandhya
i added your said line to my controller but nothng seems to happen in the VF page, do i need to add anything else in the VF page to display the date
Regards
Soumyodeep
You are not passing "MovieDate" from your vf Page instead you are assigning it to "Ticket_transaction__c.Movie_Date__c"
Try like this should work for you And in your class you are missing some syntax as well at line number 14 (where Movie_Date__c=:MovieDate')
Hope your issue is resolved.
Regards,
Magesh
Ticket_transaction__c.Movie_Date__c is the date type field that needs to be queried. i have not defined any date type field MovieDate in the object.
also the where Movie_Date__c='+MovieDate is another way of passing varible to SOQL query other than the way you have mentioned, and i have tried out where Movie_Date__c='+MovieDate also but it does not work.
What i believe is the issue is the formatting of the date field due to which its not working, can you suggest any thing on that.
Regards
Soumyodeep
You dont need any field for the "MovieDate" variable and also the you were using at line 14 (where Movie_Date__c=:MovieDate') which is incorrect because Database.query accepts only string not like this "=:MovieDate".
OK here i have updated you VF page and class. Just try the below code and let me know
Class:
i am getting the error as below
Error: Could not resolve the entity from <apex:inputField> value binding '{!MovieDate}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable.
ok I see
lets modify your class and VF like this
Help for this Page
System.QueryException: line 1:125 no viable alternative at character '<EOF>'
Error is in expression '{!MovieList}' in component <apex:commandButton> in page bookticket: Class.MovieController.MovieList: line 14, column 1
Class.MovieController.MovieList: line 14, column 1
Can you replace line 14 with
Mlist= [Select Movie__c, Theater__c, Show_TIme__c, Movie_Language__c from Movie_at_Theater__c where Movie_Date__c=:tc.Movie_Date__c];
and for 19 as well
list<Movie_at_Theater__c>Templist= [Select Movie__c, Theater__c, Show_TIme__c, Movie_Language__c from Movie_at_Theater__c where Movie_Date__c=:tc.Movie_Date__c];
Magesh, is it possible to have a skype call later today, so that we can have a better understanding of the issue