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.

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();
  • 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/…) – Renato Oliveira Jun 17 '16 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 '16 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. – evankincade Jun 19 '16 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). – Renato Oliveira Jun 19 '16 at 20:10
  • @RenatoOliveira Thank you for the information! I appreciate it. – evankincade Jun 19 '16 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.

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.