Calculate sum of flow variable within a for each loop

graeme1983
Giga Contributor

Hi guys quick question. I do look up which returns (in this case) two records and for each item I set the flow variable to the total time (Duration) and I wish to add these flow variable iterations together to give a total sum. The flow variable result for each item in the look up is 10 and 42 respectively so I wish to return a total of 52. Can anyone advise on how I can do this please? I know it has to be simple, I just get solution it. Thank you so much

1 ACCEPTED SOLUTION

Animesh Das2
Mega Sage

Hi @graeme1983 ,

 

If I understood correctly, at the end of each loop you want the total sum of each total duration. You may try below steps as mentioned sequentially to configure the flow and its actions. Hopefully it would work for you.

1. You can create two flow variables of Integer type, 'totalDuration' and 'totalSumDuration'.

2. Before 'Look up records' step you can set the 'totalSumDuration' flow variable to 0.

3. Then, inside 'For each item' step (within loop) first set the 'totalDuration' flow variable (this will hold 10 in first iteration and 42 in 2nd iteration).

4. Next, set the 'totalSumDuration' flow variable with script as below,

 

var sum = (fd_data.flow_var.totalSumDuration + fd_data.flow_var.totalDuration);
return sum;

 

Whatever value the 'totalSumDuration' flow variable holds, which is the total of all the durations, after all the iterations are complete, you can use that total value outside the for each loop as well as per your need.

 

If this address your question, please don't forget to mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das

View solution in original post

3 REPLIES 3

Animesh Das2
Mega Sage

Hi @graeme1983 ,

 

If I understood correctly, at the end of each loop you want the total sum of each total duration. You may try below steps as mentioned sequentially to configure the flow and its actions. Hopefully it would work for you.

1. You can create two flow variables of Integer type, 'totalDuration' and 'totalSumDuration'.

2. Before 'Look up records' step you can set the 'totalSumDuration' flow variable to 0.

3. Then, inside 'For each item' step (within loop) first set the 'totalDuration' flow variable (this will hold 10 in first iteration and 42 in 2nd iteration).

4. Next, set the 'totalSumDuration' flow variable with script as below,

 

var sum = (fd_data.flow_var.totalSumDuration + fd_data.flow_var.totalDuration);
return sum;

 

Whatever value the 'totalSumDuration' flow variable holds, which is the total of all the durations, after all the iterations are complete, you can use that total value outside the for each loop as well as per your need.

 

If this address your question, please don't forget to mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das

Thank you so much sir. I banged my head off a wall for hours trying to figure it out. I really appreciate you taking the time to respond so thoroughly 🙂 Thanks again Animesh.

Ankur Bawiskar
Tera Patron
Tera Patron

@graeme1983 

you can follow steps mentioned by Animesh

let us know if stuck somewhere

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader