1

Can anyone tell me how to use the Date Literal in apex?
My use case is I want to use Date literal as a custom picklsit.
I want to create custom picklist as follows
enter image description here

Please someone tell me how can I implement,

Thanks in advance,
Karthick

2 Answers 2

2

It sounds like your question is regarding how to create a custom picklist in a visualforce page. You can build the list in your control using apex or you can do so in your visualforce page. I prefer creating the list client side.

You can do something like this:

<apex:page>
  <span style="font-weight:bold;">Date</span>
  <select id="dateLiteral">
    <option value="Select">Select</option>
    <option value="Yesterday">Yesterday</option>
    <option value="Today">Today</option>
    <option value="Last_Week">Last_Week</option>
    //Other options here.
  </select>
</apex:page>

<script>
  function getDate(){
    document.getElementById("dateLiteral").value;

    //Do something here with the value.
    //Perhaps invoke an actionFunction to pass value to controller or JS remoting.
  }
</script>

Don't forget to validate user input server side.

************* One possible working solution is below. *********************

<apex:page controller="testController">

  <script>
    function getDate(){
      var date = document.getElementById("dateLiteral").options[document.getElementById("dateLiteral").selectedIndex].text
      Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.testController.cons}',
        date,
        function(result, event) {
          for(var i = 0; i < result.length; i++){
            document.getElementById("resultdiv").innerHTML += result[i].Name + '<br/>';
          }
        }
      );
    }
  </script>

  <apex:form >
    <span style="font-weight:bold;">Date</span>
    <select id="dateLiteral">
      <option value="Select">Select</option>
      <option value="Yesterday">Yesterday</option>
      <option value="TODAY">TODAY</option>
      <option value="Last_Week">Last_Week</option>
      //Other options here.
    </select>
    <apex:commandbutton value="Submit" onclick="getDate();return false;"/>
    <div id="resultdiv"/>
  </apex:form>
</apex:page>

public with sharing class testController {

    @remoteaction
    public static list<contact> cons(string value){
       list<contact> cs = new list<contact>();
       cs = Database.query('SELECT Name FROM contact WHERE createddate = '+ value +'');
     return cs;
     }

}

This solution uses JavaScript remoting which is very efficient because it is stateless. This is one of many possible solutions. The easiest way to get your query to work is by using dynamic soql.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_soql.htm

18
  • My question is how to use date literal in apex Commented Apr 4, 2015 at 12:32
  • I want all the date literal as custom picklist Commented Apr 4, 2015 at 12:32
  • what is the formula for this Month,Last Month,this year,Last year,This quarter and Last quarter Commented Apr 4, 2015 at 12:33
  • Using dynamic soql is the easiest way to get your query to work since you don't necessarily have to worry about date formatting. See my revised answer for working solution. Please mark as best answer if this is what you're looking for.
    – Berg
    Commented Apr 4, 2015 at 13:25
  • it doesnt work because you are passing string...but in where condtion yor r checking with date datatype Commented Apr 4, 2015 at 13:35
0

date literals are used in SOQL. You would pass it to your controller to used in SOQL query.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .