Solved

Create loop over date range

  • 31 July 2019
  • 2 replies
  • 72 views

Dear all

 

I have a form with two date fields: start and end date.
Now I need to create a sharepoint-list item for every single day from start-date to end-date. How can I create this loop?
I searched the k2 community but I only found a link to a "How to create a loop" that isn't working anymore and a similar question from 2018 that isn't answered.
(https://community.k2.com/t5/K2-Five/K2-Five-Workflow-Loop-through-Specified-date/m-p/106347)


Regards
Viktor

icon

Best answer by skb_ev 1 August 2019, 10:54

View original

2 replies

Dear all

 

I managed to solve this by myself. But it took me a long time!

As mentioned I have a start-date and a stop-date. For every single day from start until the stop date I want to do something.

  1. In the workflow I set a variable with the result of the function "day difference".
  2. With this value I'm using a "create reference" step and do a "Get List Items" of a smart object of a "helper list" in sharepoint. This list only contains one column with the numbers 1 to 31. For the "Get list items" I am using the "day difference" as a filter (Column number is less than day difference). The output is a list of numbers (starting from 1) until the "day difference". I called this reference "Helper items"
  3. With this "helper items" list I'm using a loop-step and for each loop of the loop step I do the needed action for one day

et voila: A loop over a date range is ready

 

Additional Infos:

@function "day difference": I learned that the function "date difference" returns comma-values. If the start and the end-date are on the same date (for example from 10am to 8pm then the return value is 0.4!! So I had to use the "to Integer" functio

@step "loop": If the "day difference" value is 0 than the loop-step is never run and the workflow continues with the next step on the "complete" path of the loop-step. So I had to do one extra step to handle the complete date range because either the first or last day of the date range was allways missing. I decided to do the needed action once before creating the reference and using the loop-step to handle the case "date difference = 0".

 


Better alternative for the "@step loop" above:

I change my workflow and changed the filter of the "create reference" step. The filter is now:

   "column number" < "Formula (Daycounter +1)"

 

With that filter I don't have the problem anymore that there is not "less than equal" in the filter available.

For the loop action I can now calculate the single dates with this formula:

   "start-date" + "column number"

Because the column number starts with 0 we can iterate realy from the start to the end date.

 

Reply