function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Renata Cruz 9Renata Cruz 9 

button javascript to compare date

Hi,

I am trying to campare dates in a button javascript, but it doesn't work. Can anyone help me?

Code:
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/36.0/apex.js")} 

var tempdate = new Date("{!Case.Next_action_required_date__c}");
var agora = "{!NOW()}";

if( tempdate < agora)

alert("Please, inform the Next Action Required Date")
Best Answer chosen by Renata Cruz 9
Varun SinghVarun Singh
Hi
I tried  this  code  for  Date time for  filed
Next_action_required_date__c is date time  type

I am  getting all alert in case detail page  when  detail button will clicked
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 
var dealReg = new sforce.SObject("case"); 
dealReg.id = "{!Case.Id}"; 
var expirationDate = "{!Case.Next_action_required_date__c}"; 
alert(expirationDate.toString());

var myDate = new Date(expirationDate); 
alert(myDate);
var agora = "{!NOW()}";

var date = new Date(agora).getTime();;

if(myDate<date)
{
alert('Please, inform the Next Action Required Date' + myDate);
}
 
window.location.reload();

 

All Answers

RD@SFRD@SF
Hello Renata,

The code
var tempdate = new Date("{!Case.Next_action_required_date__c}");
is the cause of the issue, the javascript function Date accepts parameters in a different way.
Please follow the link (https://www.w3schools.com/js/js_dates.asp)to know more.

You would have to update that code accordingly.

If your requirement is not very complex i.e. Users all work in same timezone and all

try this bit of code
var tempdate = "{!Case.Next_action_required_date__c}";

Hope it helps
RD



 
Varun SinghVarun Singh
Hi Renata

We are using date  and  date time filed .
first  you will sure  which kind  off  filed  you are using

i have  create alert  for  opportuntiy you can  impolement your  logic  from this  code

This is opp  detail  button.
todat  date output from {!NOW}--> 10/4/2017 9:07 AM 
close date output  is:   Wed Sep 27 2017 00:00:00 GMT+0530 (India Standard Time)​

So please check data  is  returning date in date  format  ot  datetime!


 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 
var dealReg = new sforce.SObject("opportunity"); 
dealReg.id = "{!Opportunity.Id}"; 
var expirationDate = "{!Opportunity.CloseDate }"; 
var myDate = new Date(expirationDate); 
myDate.setDate(myDate.getDate() + 180); 
alert('my date '+myDate); 
var agora = "{!NOW()}"; 
alert('today'+agora); 
window.location.reload();

I hope  is  this  helpful!
Thanks
varun
Renata Cruz 9Renata Cruz 9
Varun,

I tried your example, but i get the error: Invalid Date.

See my code:

var agora = "{!NOW()}";
var expirationDate = "{!Case.Next_action_required_date__c}"; 
var myDate = new Date(expirationDate); 
myDate.setDate(myDate.getDate()); 

if( myDate < agora)

alert("Please, inform the Next Action Required Date" + myDate)
Renata Cruz 9Renata Cruz 9
RD@SF,

I have already tried your suggestion before, but didn't work.
Thank you!!
Varun SinghVarun Singh
Hi Renata 

I fixed the code  
You will get right result  

Use  the same code in button.

For your information i have created  Next_action_required_date__c in my case object as Date type
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 
var dealReg = new sforce.SObject("case"); 
dealReg.id = "{!Case.Id}"; 
var expirationDate = "{!Case.Next_action_required_date__c}"; 
var myDate = new Date(expirationDate).getTime();; 

var agora = "{!NOW()}";

var date = new Date(agora).getTime();;

if(myDate<date)
{
alert('Please, inform the Next Action Required Date' + myDate);
}
 
window.location.reload();

If information is  helpful select my answer as best answer!
Thanks
Varun
Renata Cruz 9Renata Cruz 9
Hello Varun,

I have created the button with your code, but didn't work.
The field Next_action_required_date__c is a Date/Time format.

Thank you!
Varun SinghVarun Singh
Hi
I tried  this  code  for  Date time for  filed
Next_action_required_date__c is date time  type

I am  getting all alert in case detail page  when  detail button will clicked
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 
var dealReg = new sforce.SObject("case"); 
dealReg.id = "{!Case.Id}"; 
var expirationDate = "{!Case.Next_action_required_date__c}"; 
alert(expirationDate.toString());

var myDate = new Date(expirationDate); 
alert(myDate);
var agora = "{!NOW()}";

var date = new Date(agora).getTime();;

if(myDate<date)
{
alert('Please, inform the Next Action Required Date' + myDate);
}
 
window.location.reload();

 
This was selected as the best answer
RD@SFRD@SF
Hi Renata,

1. Try putting an js alert with the expiration date. What do you get here?
2. Check the system timezone and the salesforce user timezone, just shooting in the dark here

RD
Renata Cruz 9Renata Cruz 9
Hi Varun,

I have tested your code, but in the line "alert(myDate);" showed the error: Invalid Date.
Varun SinghVarun Singh
can  you check  Next_action_required_date__c have some  value  or  it  is blank.?
Renata Cruz 9Renata Cruz 9
Hi Varun, 
Thanks.. I have inserted a verification in this field to verified if it has value.

Now is working with the code below:

{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/36.0/apex.js")} 

var dealReg = new sforce.SObject("Case"); 
dealReg.id = "{!Case.Id}"; 
var expirationDate = "{!Case.Next_action_required_date__c}"; 
if(expirationDate != ""){ 
var nextAction = new Date(expirationDate).toISOString(); 


var startDate = "{!NOW()}"; 
var sDate = new Date(startDate).toISOString(); 

if(nextAction<sDate) 

alert('Please, inform the Next Action Required Date'); 


Thank you Varun and RD@SF to help me!!