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
Jai SureshJai Suresh 

Custom Save button to create multiple related records

This is going to be tricky.

I have two related objects.

ObjOne has two date Fields StartDate, EndDate
ObjTwo has a lookup to ObjOne and Date, a date field

When I save ObjOne, I need to find the number of days between the two dates and create that many ObjTwo records, each populated with a date (starting StartDate till EndDate).

My trouble is with the dates. Any help is welcome.

Thank you
Swati GSwati G
Hi, 

First get the difference between the dates and then iterate loop till that many days.

Example: 

Integer diffDays = startDate.daysBetween(EndDate);
List<ObjTwo> obj2List = new List<ObjTwo>();

for(Integer i = 0;i < diffDays; i++){
   obj2List.add(new ObjTwo(data = startData.addDays(i) , ObjOne = ObjOne.Id )); // this will start with start date till end date.
}

insert obj2List;
Jai SureshJai Suresh
Thanks Swati for the fast reply. 

But I failed to mention an important caveat, I have to do this with javascript, that is why I said I am having trouble with dates. 

Offtopic: Is there a way to edit our posts?
Swati GSwati G
Why you want to implement in javascript? You can also write code on controller and call it from javascipt.