function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ChristineVWChristineVW 

Problems Setting Dates Dynamically (date created + 10) using Javascript ?

I'm having difficulty with this scenario:  I have a custom button that uses onclick Javascript to create 14 custom object records.  I want each of the custom object records that is created to have a different value in the field Planned Date.  The value should be Created Date + x where x varies across the 14 records. 

Here's the code that isn't working.  (It works fine if I take out the + # and sets Planned Date to the current date for every record.)  I'm trying to add a given number of days to the day on which the record in this batch was created to determine and set the Planned Date of each record: 


var plans = [];

var plandate = new Date();

plans[0] = plandate.setDate(plandate.getDate() + 4);
plans[1] = plandate.setDate(plandate.getDate() + 10);
plans[2] = plandate.setDate(plandate.getDate() + 17);
plans[3] = plandate.setDate(plandate.getDate() + 24);
plans[4] = plandate.setDate(plandate.getDate() + 35);
plans[5] = plandate.setDate(plandate.getDate() + 14);
plans[6] = plandate.setDate(plandate.getDate() + 40) ;
plans[7] = plandate.setDate(plandate.getDate() + 40);
plans[8] = plandate.setDate(plandate.getDate() + 14);
plans[9] = plandate.setDate(plandate.getDate() + 30);
plans[10] = plandate.setDate(plandate.getDate() + 35);
plans[11] = plandate.setDate(plandate.getDate() + 50);
plans[12] = plandate.setDate(plandate.getDate() + 64);
plans[13] = plandate.setDate(plandate.getDate() + 70);

for (var i=0; i<14; i++) {
    var object = new sforce.SObject("Object__c");
...other code...
    object.Planned_Date__c = plans[i];
...other code...
cheenathcheenath
> I want each of the custom object records that is created to have a different value in the field Planned Date.

You are creating only one instance of the date object and setting
different date on it. If you want different date values for each date in the
array, you have to create new date instances, eg:

var plans = [];

var plandate = new Date();

plans[0] = new Date();
plans[0].setDate(plandate.getDate() + 4);

plans[1] = new Date();
plans[1].setDate(plandate.getDate() + 10);
...


ChristineVWChristineVW
Thank you very much, problem solved :smileyhappy: