I am currently creating a custom button on a custom object in Salesforce that will create a new record of that same object with some updates. One of those updates is to capture the current Effective Date from the existing record and add one year to it on the new record. I'm fairly new to JS, and I can't seem to find anything on this as well so I figured I'd ask here. Here's what I have (aside form the date issue, everything else works fine. The funny thing is that the way it's currently written creates a record with an effective date in the year 1969...)


var rmopp = new sforce.SObject('Risk_Management_Opportunity__c');

var effectivedate = new Date({!Risk_Management_Opportunity__c.Effective_Date__c})

rmopp.name = '{!Risk_Management_Opportunity__c.Name}- Renewal';
  rmopp.Account__c = '{!Risk_Management_Opportunity__c.AccountId__c}'; 
    rmopp.Effective_Date__c = effectivedate
      rmopp.Stage__c = 'Closed Won';
        rmopp.Courses__c = '{!Risk_Management_Opportunity__c.Courses__c}'; 
          rmopp.Topics__c = '{!Risk_Management_Opportunity__c.Topics__c}';

var result = sforce.connection.create([rmopp]);


window.location = "/" + result[0].id + "/e";


alert('Could not create record '+result);


1 Answer 1


Your problem starts around here:

var effectivedate = new Date({!Risk_Management_Opportunity__c.Effective_Date__c})

JavaScript allows four constructors, one having no parameters (which returns "now"), one by a "date string" (by user locale), one by "the number of milliseconds since January 1st, 1970 at 00:00:00.000-00:00 GMT", and one by multiple parameters (year, month, day, etc).

Since you didn't use quotes, it gets put out as the literal source code:

new Date(6/29/2015);

Which causes it to be treated as ordinary division, looking something like this to the browser:

new Date(0.000102...);

Your 0.0001 is rounded to the nearest whole millisecond, or, in other words, 0, then thrown into the Date constructor:

new Date(0);

So, when it gets saved, it will render as that exact moment in time. Naturally, if you live in a negative time zone (those West of London, such as anywhere in the United States), this will render as December 31st, 1969.

To get the date into the proper format, you have a few choices. First, you could just add the quotes:

var effectivedate = new Date("{!Risk_Management_Opportunity__c.Effective_Date__c}")

This is probably the most straight-forward method, but will also have complications if the browser's date format doesn't match the user's Salesforce locale. For example, "06/07/2015" will be rendered as June 7th or July 6th, depending on the user's date format.

To be 100% certain that there's no complications, use the date's full form:

var effectivedate = new Date({!YEAR(Risk_Management_Opportunity__c.Effective_Date__c)}, {!MONTH(Risk_Management_Opportunity__c.Effective_Date__c)-1}, {!DAY(Risk_Management_Opportunity__c.Effective_Date__c)});

Please note that the -1 is necessary because MONTH returns 1-12, and new Date(y, m, d) in JavaScript needs 0-11.

As a side note, you can safely just "add one year" directly into this constructor, as JavaScript will automatically adjust dates that are out of bounds to proper dates.

For example, new Date(2013, 1, 29) (2012 was a leap year) correctly returns March 1st, 2013 (remember, "1" is February in JavaScript). So, you might simply want to do this:

var effectivedate = new Date({!YEAR(Risk_Management_Opportunity__c.Effective_Date__c)+1}, {!MONTH(Risk_Management_Opportunity__c.Effective_Date__c)-1}, {!DAY(Risk_Management_Opportunity__c.Effective_Date__c)});
  • Thanks a ton sfdcfox! The explanation definitely makes sense.
    – user6966
    Jun 29, 2015 at 23:10

You must log in to answer this question.

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