+ Start a Discussion
cmtgolf05cmtgolf05 

Dynamic SOQL grabbing date from URL

I am new to APEX and SOQL however I have been able to figure out of what most I need to know. That being said I have now become stuck.

 

I am developing a Visualforce page that displays results from my Query. Currently the results are over 1000 items so I need to narrow my search down and I want the user of the page to be able to set date ranges that narrows the results down. My thought is that the user can set the date range and that will load into the URL and my SOQL query will grab the date ranges from the URL. However I am unable to make this work.

 

Here is my SOQL code

 

SELECT Name FROM Opportunity WHERE CloseDate = :ApexPages.currentPage().getParameters().get('CloseDate')

 

However I get an error trying to save this code: Invalid bind expression type of String for column of type Date.

 

The code above works perfectly when grabbing the opportunity number form the url however I cannot get it to work for the date.

 

Any help?

Best Answer chosen by Admin (Salesforce Developers) 
Brad BumbaloughBrad Bumbalough

Would this work?

 

Date myDate = Date.valueOf(ApexPages.currentPage().getParameters().get('CloseDate'));

List<Oppertunity> myOpps = [SELECT Name FROM Opportunity WHERE CloseDate = :myDate];

 

Is CloseDate a DateTime or just a Date? because you may need to do <= instead of =. 

All Answers

Brad BumbaloughBrad Bumbalough

Would this work?

 

Date myDate = Date.valueOf(ApexPages.currentPage().getParameters().get('CloseDate'));

List<Oppertunity> myOpps = [SELECT Name FROM Opportunity WHERE CloseDate = :myDate];

 

Is CloseDate a DateTime or just a Date? because you may need to do <= instead of =. 

This was selected as the best answer
cmtgolf05cmtgolf05

That worked nicely.

 

Thank you very much!

AshlekhAshlekh

Hi,

 

In your query if your using static query statement query, then  you are comparing  date/datetime field with string value.

 

Because when ever we get the value from url parameters then It always in string format. 

 

Your closedate and url parameter value is not compatable. so for this you have to get this parameter value in string varibale and then create a new instance of Date/datetime using this string value.Then execute the query.

 

Thanks 

Ashlekh

 

if  this post help you then mark it as a solution and don't forget to give me kudo's .