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?

1 Like

Looks like you are conflating server session variables and server values.

You are setting a value of set_session_date, but are using a session variable of date in your while loop…use one or the other (to me it doesn’t appear session storage would be needed.)

If that doesn’t completely fix, you may need to convert some of these to actual date objects using the formatters.

@mebeingken Thank you for the reply and explanation.

You were right. I was confusing variables and server session variables.

After changing the While Step Set value step to set the value set_session_date+6 days the while loop loops correctly.