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
mike1051mike1051 

javascript date issue on custom button isssue..?

When i click on this custom button i need to fill a field with the current date and time.But some times its giving me a previous date i.e exactly 1 day prior to current date..i dont know why this is happpening..can some help me out in this..may be there is some issue with javascript...or timezone issue..as its not happening everytime...but sometime..

 

 

var SO = new sforce.SObject("Sales_Order__c");

    function fixTime(time){
       if(time < 10) {time = "0" + time};
       return time;
    }
     function fixDate(date){
      var Month = fixTime(date.getMonth() + 1);
      var Day = fixTime(date.getDate());
      var UTC = date.toUTCString();
      var Time = UTC.substring(UTC.indexOf(':')-2, UTC.indexOf(':')+6);
      var Minutes = fixTime(date.getMinutes());
      var Seconds = fixTime(date.getSeconds());
      return date.getFullYear() + "-" + Month + "-" + Day + "T" + Time;  
    }

    SO.date__c = fixDate(new Date());
updateSO = sforce.connection.update([SO]);

 

sfdcfoxsfdcfox

You're getting the local time zone year, month and day, but UTC time hours and minutes. When it's today here and tomorrow in UTC, your date calculations will become a day behind. Example follows:

 

Local time: 2013-12-31 22:00 -05:00

UTC time: 2014-01-01 03:00 -00:00

 

Year = 2013

Month = 12

Day = 31

Time = 03:00

 

So, you're committing the value of '2013-12-31T03:00Z' instead of '2014-01-01T03:00Z'.

 

There is no need for any of this code at all. The function sforce.internal.dateTimeToString automatically converts the date/time coordinate to the correct value for you as long as it is a native Date object.

 

In other words, change your code to:

 

var SO = new sforce.SObject("Sales_Order__c");
SO.Date__c = new Date();
updateSO = sforce.connection.update([SO]);

Do not call sforce.internal.dateTimeToString, as it is already called for you, and as the "internal" suggests, you should not be using that function explicitly.

 

mike1051mike1051

Thnaks for you answer mate..

I am getting an error when using this.Its giving an fault string error..

sfdcfoxsfdcfox
The fault is elsewhere. In this case, it's (now) obvious that you forgot to include the record id, so update() would fail because there is no ID. The dates work perfectly fine, I assure you.