3

I have a Visualforce page in which I try to compare 2 dates.

The code:

<apex:page StandardController="Account">
<script>
window.document.onload = new function(e)
{
  alert("{!Account.Support_End_Date__c}");
  alert("{!NOW()}");

  if({!Account.Support_End_Date__c} <= {!NOW()})
  {
    alert("Customer is NOT under Support.");
  }
}
</script>

</apex:page>

Is not working correctly.

I want the pop up to display if the support_end_date is smaller than today/now.

UPDATE

by changing my code to the following (sorry for duplication, I think it may help others)

  if("{!Account.Support_End_Date__c}" <= "{!NOW()}")
  {
    alert("Customer is NOT under Support \nor not date is given.");
  }

I get the script to complete successfully, however, the behavior is not as requested - both dates are returned as formatted strings. How do I compare them?

6

The javascript is trying to compare the dates as strings. You should convert Account.Support_End_Date__c to a javascript Date object:

var tempdate=new Date('{!Account.Support_End_Date__c}');

if(tempdate < Date.now()){
    //Do Stuff
}

This should give you

<apex:page StandardController="Account">
<script>
window.document.onload = new function(e)
{
    var tempdate=new Date('{!Account.Support_End_Date__c}');

    if(tempdate < Date.now()){
        alert("Customer is NOT under Support.");
    }
}
</script>

</apex:page>
5
  • thanks, but something is wrong with the first line of code. doesn't compute to nothing, and no errors are thrown. BTW: where do I see VSFpages error?
    – Saariko
    Aug 16 '12 at 11:22
  • 1
    I have created a VF page and tested and the error was that I hadnt encased the {!Account.Support_End_Date__c} in quotes. Ive updated my answer
    – Jon Hazan
    Aug 16 '12 at 12:34
  • VOOT VOOT the fixed line should have "" around the Apex value. donno why, but that works. var tempdate = new Date("{!Account.Support_End_Date__c}");
    – Saariko
    Aug 16 '12 at 12:39
  • btw, how did you get to see the errors?
    – Saariko
    Aug 16 '12 at 12:41
  • 1
    Using Chrome press f12 to bring up the development toolbar (Firefox use the Firebug plugin), I then switched to the console tab and it showed a generic iframe error(Embeded VF Page). Using the HTML viewer I had a look at the generated iframe code. From there I copy pasted the line into the console window and ran it to see the error. (Long reply sorry but hope it makes sense)
    – Jon Hazan
    Aug 16 '12 at 12:46
3

I would go a different route. Create a simple Apex controller. Write a simple getter such as public Boolean isSupportEndDateToday { get{ return acct.Support_End_Date__c == Date.today(); } } and then reference that boolean in your JavaScript.

The whole point of MVC is to retain your controlling logic in your controller and not in the view. This also prevents anything that may happen across funky browser differences.

1
  • 2
    In addition, putting this sort of validation on the server stops malign users from subverting your logic. You probably don't want the user to be able to just view the page and use Greasemonkey to invoke whatever logic would be executed if they were still in support!
    – metadaddy
    Aug 17 '12 at 0:55
2

I realize that this is an old question, but maybe this answer will help someone who finds it in a search.

Note that your comparison is done by the Javascript:

if({!Account.Support_End_Date__c} <= {!NOW()})

You could do the comparison in Visualforce by using a single set of braces {!...}.

if({!Account.Support_End_Date__c <= NOW()})

That should evaluate to either if(true) or if(false).

-1
<apex:page StandardController="Account">
<script>
window.document.onload = new function(e)
{
    var tempdate=new Date('{!Account.Support_End_Date__c}');
    var TodayDate=new Date('{!Today()}');

    if(tempdate < TodayDate){
        alert("Customer is NOT under Support.");
    }
}
</script>

</apex:page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.