2

I'm trying out a new Custom Button with javascript for the Opportunity List View page that would extend the close date of any selected records +15 Days. I found code here that I was working to modify to my use case: http://www.interactiveties.com/b_execute_javascript_button.php#.U59ikvldVAI

Here is my code... Im getting an error when trying it out of "Unexpected Token ;"

Any ideas?

*/
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} //adds the proper code for inclusion of AJAX toolkit
var url = parent.location.href; //string for the URL of the current page
var records = {!GETRECORDIDS($ObjectType.Opportunity)}; //grabs the Opportunity records that the user is requesting to update
var updateRecords = []; //array for holding records that this code will ultimately update

if (records[0] == null) { //if the button was clicked but there was no record selected
    alert("Please select at least one record to update."); //alert the user that they didn't make a selection 
} else { //otherwise, there was a record selection
    for (var a=0; a<records.length; a++) { //for all records
        var update_Opportunity = new sforce.SObject("Opportunity"); //create a new sObject for storing updated record details
        update_Opportunity.Id = records[a]; //set the Id of the selected Lead record  
        update_Opportunity.CloseDate = {'!Opportunity.CloseDate} + 15'; //set the value for Closed Date to existing date and 15 days
        updateRecords.push(update_Opportunity); //add the updated record to our array
    }
    result = sforce.connection.update(updateRecords); //push the updated records back to Salesforce
    parent.location.href = url; //refresh the page
}

UPDATE: Final Code used:

/*  
    NOTES: 
        - Updates selected Opportunity records Close Date plus 14 Days from Today
        - Display Type is "List Button"
        - Select Display Checkboxes (for Multi-Record Selection) option is selected
        - Behavior is "Execute JavaScript"
        - Content Source is "OnClick JavaScript"
*/
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} //adds the proper code for inclusion of AJAX toolkit
var url = parent.location.href; //string for the URL of the current page
var records = {!GETRECORDIDS($ObjectType.Opportunity)}; //grabs the Opportunity records that the user is requesting to update
var updateRecords = []; //array for holding records that this code will ultimately update

if (records[0] == null) { //if the button was clicked but there was no record selected
    alert("Please select at least one record to update."); //alert the user that they didn't make a selection 
} else { //otherwise, there was a record selection
    for (var a=0; a<records.length; a++) { //for all records
        var update_Opportunity = new sforce.SObject("Opportunity"); //create a new sObject for storing updated record details
        update_Opportunity.Id = records[a]; //set the Id of the selected Lead record  
        update_Opportunity.CloseDate; var d = new Date({!Opportunity.CloseDate});
d.setDate(d.getDate() + 14);
update_Opportunity.CloseDate = d; //set the value for Closed Date to existing date and 14 days
        updateRecords.push(update_Opportunity); //add the updated record to our array
    }
    result = sforce.connection.update(updateRecords); //push the updated records back to Salesforce
    parent.location.href = url; //refresh the page
}
2
  • This {'!Opportunity.CloseDate} + 15'; looks wrong; try commenting that line out and if that is the problem then work on how to add the days.
    – Keith C
    Jun 16, 2014 at 22:48
  • Thanks for the update this code worked perfectly for a use case I had.
    – Dan
    Apr 14, 2016 at 16:04

1 Answer 1

2

Something like this is needed to go from an Apex date object rendered on the server to a JavaScript date object on the client that date arithmetic can then be done on:

var d = new Date({!Opportunity.CloseDate.time});
d.setDate(d.getDate() + 15);
update_Opportunity.CloseDate = d;

(The Apex DateTime.getTime returns the number of milliseconds since January 1, 1970, 00:00:00 GMT and that value can be used in one of JavaScript's Date constructors.)

1
  • That worked - I just did CloseDate without the time though since its not a date/time field. Jun 17, 2014 at 15:34

You must log in to answer this question.

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