0

I'm going through one of the Force.com workbooks and attempting to add some functionality to a Date field using a custom button.

On click, I want to button to update the appropriate field by moving the Date a year forward. Here is my code:

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

var c = new sforce.SObject("Invoice Statement"); 
c.id ="{!Invoice_Statement__c.Id}";
c.{!Invoice_Statement__c.Contract_Date__c} = new Date();
newRecords.push(c); 

result = sforce.connection.update(newRecords); 

window.location.reload();

Can someone point me in the right direction of what I'm doing wrong here? most of the things I've tried have resulted in an "Unexpected number" error.

2 Answers 2

5

It would look something like this:

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

var c = new sforce.SObject("Invoice_Statement__c"); 
c.id = "{!Invoice_Statement__c.Id}";
c.Contract_Date__c = new Date("{!Invoice_Statement__c.Contract_Date__c+365}");
newRecords.push(c); 

result = sforce.connection.update(newRecords); 

window.location.reload();
6
  • To clarify: when you instantiate an object through the JavaScript Toolkit you need to pass the object's API Name, but also the Date field must be formatted accordingly (see: developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/…) Jun 17, 2016 at 22:45
  • Is this going to work for any leap year? I suggest for safety to use var dt = new Date("{!Invoice_Statement__c.Contract_Date__c}"); dt.setFullYear(dt.getFullYear()+1); Invoice_Statement__c.Contract_Date__c = dt;
    – Andrei.Z
    Jun 17, 2016 at 23:05
  • @RenatoOliveira Do I need to format it if I'm not performing a SOQL query? I want to modify the value of the field by adding a year to it once the custom button is pressed. Jun 19, 2016 at 20:08
  • No, you'll get it pre-formatted using sfdcfox's solution. I just said that to make clear how datetime fields work through the toolkit. You would need to do that formatting if you hadn't the value already (for example, if you wanted the exact time the user clicked the button). Jun 19, 2016 at 20:10
  • @RenatoOliveira Thank you for the information! I appreciate it. Jun 19, 2016 at 20:35
0

everyone. Just wanted to say that I've come up with a working solution. Take a look below:

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

var c = new sforce.SObject("Invoice_Statement__c"); 
c.id ="{!Invoice_Statement__c.Id}";
c.Contract_Date__c = new Date("
{!YEAR(Invoice_Statement__c.Contract_Date__c) + 1}, 
{!MONTH(Invoice_Statement__c.Contract_Date__c)},
{!DAY(Invoice_Statement__c.Contract_Date__c)}");
newRecords.push(c); 

result = sforce.connection.update(newRecords); 

window.location.reload();

Part of the problem was that when I was assigning the Invoice Statement to var c, I wasn't calling it correctly. I was using "Invoice Statement" instead of "Invoice_Statement__c". Once I fixed that issue, I ran into an issue with improperly formatting the date, which is why I chose to declare the year/month/day as I did.

You must log in to answer this question.

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