1

everyone.

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:

{!requireScript("/soap/ajax/37.0/connection.js")}
{!requireScript("/soap/ajax/37.0/apex.js")}
var newRecords = [];

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

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

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

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

newRecords.push(c);

result = sforce.connection.update(newRecords); 

window.location.reload();

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

2

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)}, " +
                       "{!DAY(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.