+ Start a Discussion
shan876shan876 

How do I loop through in ajax???

Hi the following is my code for a submit button:
Code:
z_cal.prototype.submitSelections = function () 
//PRIVATE: submit the calendar's selection variables 
{ 
var dtsdate = new sforce.SObject("Holiday_Vacation__c");
//alert(this.selectedDates[0].toJSDate() + "-" + dtsdate.Opportunity__c);
for(var i=0; i<this.selectedDates.length; i++) 
 dtsdate.Date__c = (this.selectedDates[i].toJSDate());
 dtsdate.Opportunity__c = "{!Opportunity.Id}";
  
var result = sforce.connection.create([dtsdate]);

  if (result[0].getBoolean("success")) {
    alert("new account created with id/Date " + result[0]);
  } else {
   alert("failed to create account " + result[0]);
  }
}; 

 
now the thing is the user will select the dates needed off the calendar and then hit submit and with that I need it to go through the object and place in all the records in the table. I am new to javascript and ajax and salesforce. But I am trying. Can someone please help me figure out how can I loop through to get my array and the id in the table.
something like this:
OpportunityID 3/12/2005
OpportunityID 3/14/2005
OpportunityID 3/16/2005
The opportunity ID will be the same and then the calendar dates from another function goes through an array called this.selecteddates...
Thanks
z
DevAngelDevAngel
Where to start....


Looks like you want to create, what, multiple Holiday_Vacation__c records, right?  And the number of records is based on a range of user selected dates?  Not sure what toJSDate is, but that's ok.

What you want to do is create an array of Holiday_... records and submit them in a single create call.

The interesting part is that in your success? loop you are saying that x number of accounts were created.  But you are not creating accounts.

So the question before we spend anymore time on this is, what is the error that you are getting in your result?
shan876shan876

Thank you for replying back I am not getting an error. The issue is from the date array it is inserting the last date and nothing else with the opportunity ID. It looks like it goes through the loop of the array but ends up placing just the last record of the array. So I am lost on how to get the entire array in with the id.

like if they select 3/2/2007, 3/3/2007,3/4/2007 with the opportunity id 11111. It will place in 3/4/2007 and 11111.

it will not place in the other dates...

Thanks once again

Zishan

razzazrazzaz

I figured it out it was within my for loop:

Code:

z_cal.prototype.submitSelections = function () 
//PRIVATE: submit the calendar's selection variables 
{ 
var dtsdate = new sforce.SObject("Holiday_Vacation__c");
//alert(this.selectedDates[0].toJSDate() + "-" + dtsdate.Opportunity__c);
for(var i=0; i<this.selectedDates.length; i++) 
{
dtsdate.Date__c = this.selectedDates[i].toJSDate();
dtsdate.Opportunity__c = "{!Opportunity.Id}";
var result = sforce.connection.create([dtsdate]);
}
  if (result[0].getBoolean("success")) {
    alert("new account created with id/Date " + result[0]);
  } else {
   alert("failed to create account " + result[0]);
  }
};