cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Cdgf
Frequent Visitor

How to prevent For loop of adding values in list from previous items

I am trying to sum all values of a specific field in a list of opportunity lines which is working as it should. However, I am now trying to sum the values for each user linked to their opportunity lines, but it is adding all values from previous items.

 

This means that it is adding the numbers for the first user as it should, but for the next user it is adding both for the previous and current one, and on and on. 

 

How can I prevent the for loop of doing this? I have tried several things but cannot make it work.

 

I am storing the total of two different values in temporary variables, and then for each user the value is set, which will be stored in a whole new entity in Dynamics. As you can see, the values should be 40.000, 24.300 and 135.000, but instead it is adding themCapture1.PNGCapture4.PNGCapture2.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
pagonza
Community Support
Community Support

Hello @Cdgf 

 

I think you have the problem due to the variable isn't reseting.

 

May this can help you:

 

- reset the variable after update your record, set the value of the record to 0 at the final of the apply to each

- If this not work, try to don't use variables, may is possible do all the operations on the update the record?

 

Hope I can help you!

View solution in original post

2 REPLIES 2
pagonza
Community Support
Community Support

Hello @Cdgf 

 

I think you have the problem due to the variable isn't reseting.

 

May this can help you:

 

- reset the variable after update your record, set the value of the record to 0 at the final of the apply to each

- If this not work, try to don't use variables, may is possible do all the operations on the update the record?

 

Hope I can help you!

View solution in original post

Cdgf
Frequent Visitor

For some reason I had not thought about that at all and tried multiple other options, but it works perfectly! Just reset the variables after the new entity was created in the for loop

 

Thank you!

Helpful resources

Announcements
MPA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Top Solution Authors
Users online (1,187)