I'm attempting to institute an OnClick Javascript button which increases the StartDate and EndDate fields of the standard Contract object by one year (essentially renewing it), but continually receive an "Invalid or Unexpected Token" error. Here is the code below:

var newRecords = [];

var c = new sforce.SObject("Contract");

c.id ="{!Contract.Id}";

c.StartDate = new Date("{!YEAR(Contract.StartDate) + 1}, 

c.EndDate = new Date("{!YEAR(Contract.EndDate) + 1}, 


result = sforce.connection.update(newRecords); 


This code was working with a custom object in my developer organization, but doesn't seem to work on the standard Contract object.


You can't have literal line breaks in a string in JavaScript.

c.StartDate = new Date("{!YEAR(Contract.StartDate) + 1}, {!MONTH(Contract.StartDate)}, {!DAY(Contract.StartDate)}");

If you don't like the long line, use string concatenation:

c.StartDate = new Date("{!YEAR(Contract.StartDate) + 1}, " +
                       "{!MONTH(Contract.StartDate)}, " +
  • Say the "+ 1" was on the Month instead of the Year. Is there any reason why the Month being incremented wouldn't rollover and increase the year by one when the Month goes from December to January? Is Salesforce's Date data type not smart enough to recognize this? – evankincade Jul 29 '16 at 22:12
  • 1
    @evankincade You're parsing the date in JavaScript at that point; you're taking a plain number and adding 1 to it, which is then parsed by the JavaScript engine. I'd probably change the value in JavaScript directly rather than formula manipulation: c.StartDate = new Date(...); c.StartDate.setMonth(c.StartDate.getMonth()+1). setMonth is smart enough to roll over to the next year as necessary. – sfdcfox Jul 29 '16 at 22:50

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.