Question with a for loop

e_wilber
Tera Guru

I have the following script that loops over days between today and a Repeat Until date on the schedule record.

This script ALMOST works exactly as we need it but it's returning today's date as a valid option and it's not showing the last day as an option.

 

For instance, if it's run today and the Repeat Until date selected is 8/11, it returns:

*** Script: 08/03/2023 12:09:53
*** Script: 08/04/2023 12:09:53
*** Script: 08/05/2023 12:09:53
*** Script: 08/06/2023 12:09:53
*** Script: 08/07/2023 12:09:53
*** Script: 08/08/2023 12:09:53
*** Script: 08/09/2023 12:09:53
*** Script: 08/10/2023 12:09:53

We need it to skip today's date but return the actual date that was selected instead of ending a day early.

 

What change do I need to make to this script?

    var startTime = new GlideDateTime();
    var sched = new GlideRecord('cmn_schedule_span');
    sched.addQuery('sys_id', 'c98e453b1b5cf95041c20e91604bcb72');
    sched.query();

    if (sched.next()) {
        var dates  = [];
        var repeatUntil = new GlideDateTime(sched.u_repeat_until);
        gs.print(repeatUntil);

        for (var date = startTime; date.compareTo(repeatUntil) <= 0; date.addDays(1)) {
       
            gs.print(date.getDisplayValue());
        }
    }
4 REPLIES 4

nameisnani
Mega Sage

Hi @e_wilber  ,

 

The following change will make the script skip today's date and return the actual date that was selected instead of ending a day early:

var startTime = new GlideDateTime();
var sched = new GlideRecord('cmn_schedule_span');
sched.addQuery('sys_id', 'c98e453b1b5cf95041c20e91604bcb72');
sched.query();

if (sched.next()) {
  var dates  = [];
  var repeatUntil = new GlideDateTime(sched.u_repeat_until);
  gs.print(repeatUntil);

  // Skip today's date.
  date = startTime.addDays(1);

  while (date.compareTo(repeatUntil) <= 0) {
    gs.print(date.getDisplayValue());
    date.addDays(1);
  }
}

The change is to add the following line to the script:

date = startTime.addDays(1);

This line will set the date variable to the day after today's date. The loop will then start at this date and continue until the repeatUntil date is reached. This will ensure that today's date is skipped and the actual date that was selected is returned.

Here is an explanation of how the script works:

  1. The script starts by creating a new GlideDateTime object for the current date and time.
  2. The script then gets the u_repeat_until value from the schedule record.
  3. The script creates a new GlideDateTime object for the u_repeat_until date.
  4. The script loops over the days between the current date and the u_repeat_until date.
  5. For each day in the loop, the script prints the date to the console.

Please mark the answer as correct solution and helpful if helped.

Thanks for your help.

 

I tried this code but it doesn't actually look like it's making it to the loop. All it prints is the repeatUntil

e_wilber_0-1691082970311.png

 

@e_wilber  

 

I see the image you sent. The script is not making it to the loop because the today variable is not being initialized. The today variable is declared before the for loop, but it is not assigned a value. This means that the today variable is undefined when the for loop starts, and the for loop will not execute.

To fix this, you need to initialize the today variable with the current date. You can do this by adding the following code before the for loop:

var today = new GlideDateTime();

This code will initialize the today variable with the current date. Once the today variable is initialized, the for loop will be able to execute and the list of dates will be printed.

The complete script with the change is shown below:

var startTime = new GlideDateTime();
var sched = new GlideRecord('cmn_schedule_span');
sched.addQuery('sys_id', 'c98e453b1b5cf95041c20e91604bcb72');
sched.query();

if (sched.next()) {
    var dates  = [];
    var repeatUntil = new GlideDateTime(sched.u_repeat_until);
    gs.print(repeatUntil);

    var today = new GlideDateTime();

    if (today.compareTo(repeatUntil) <= 0) {
      gs.print(today.getDisplayValue());
    }

    for (var date = startTime; date.compareTo(repeatUntil) <= 0; date.addDays(1)) {
      gs.print(date.getDisplayValue());
    }
}

That's close and I'll try to tinker with it a bit more. Now it's back to including today's date and not the last day (8/11).

 

I did try adding the day to startTime at the beginning but it strangely didn't have any affect and it still starts with 8/3 and ends on 8/10 instead of start on 8/4 and end on 8/11.

 

var startTime = new GlideDateTime();
startTime.addDays(1);