Insert multiple records while condition is true

Hello,

I am working on building a patient/medical scheduling application.

In my app, patients are provided a schedule of re-occurring appointments for which I am using the calendar component. The re-occurring appointments have a start date, end date and day frequency (re-occur once/week – Monday for example).

Once the schedule with the re-occurring appointment is set, I need to create separate “scheduled” appointments records to track whether or not the patient showed up to each of those recurring appointments. The appointment records need to be generated for the schedule start date. A new record is then generated that increments to next week. This process happens until the end date is met.

So say I schedule a patient for Mondays for 5 weeks. I would expect 5 records to be generated with the date for the following 5 Mondays.

What I’ve tried so far:

  1. Form submits, start date, end date, and day of the week
  2. On submitting this record, I start a server action where I set a Server side variable to store the start date
  3. Connect to the DB
  4. Created a While loop whose condition is While the variable date <= the end date
  5. Inside the While loop, I insert a record whose date = variable date
  6. Step 2 inside the While loop, I increment the variable by 7

Then, I presume the while loop checks the condition again:
Is the variable date <= the end date? If yes, it stops.
If not, repeat.

This is what it looks like:
image

The problem I am running into is the dreaded While loop that never ends.

What am I missing here?

Community Page
Last updated: