web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / A problem for update v...
Power Apps
Unanswered

A problem for update variable during a ForAll oop

(0) ShareShare
ReportReport
Posted on by 2

This is my code

 

 

 

 

Set(remaining_amount_needed,total_amount_required);

ClearCollect(SelectedPacks, []); // This collection will store the selected packs.

ClearCollect(
 SelectedPacks,
 ForAll(
 PurchaseOptionsSorted,
 If(
 remaining_amount_needed > 0,
 {
 Drug: Drug,
 PackSize: AMT,
 UnitPrice: Unit_price,
 Order: RoundDown(remaining_amount_needed / AMT, 0),
 Quantity: RoundDown(remaining_amount_needed / AMT, 0) * AMT,
 left_quantity: remaining_amount_needed-RoundDown(remaining_amount_needed / AMT, 0) * AMT
 },
 Blank()
 );
 // Update remaining_amount_needed for the next iteration
 //UpdateContext({remaining_amount_needed: remaining_amount_needed - RoundDown(remaining_amount_needed / AMT, 0) * AMT})
 //remaining_amount_needed=remaining_amount_needed +1
 
 )
)

 

 

 

 

For each loop, I want to update the varaiable, remaining_amount_needed= its starting value - quantity 

and the loop should finish when it becomes 0

 

ChatGPT told me to add this in the end of the loop:

Set(remaining_amount_needed, remaining_amount_needed - RoundDown(remaining_amount_needed / AMT, 0) * AMT

 

but it gave me error: 

 

This function cannot be invoked within ForAll.

Categories:
I have the same question (0)
  • WarrenBelz Profile Picture
    153,084 Most Valuable Professional on at

    @baron33 ,

    You cannot set a Variable inside a ForAll function. You can however add items to a collection and then reference 

    Last(CollectionName).FieldName

    for the value you need.

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    MVP (Business Applications)   Visit my blog Practical Power Apps

  • baron33 Profile Picture
    2 on at

    Thanks.  I know I can collect the first / last record's field.

     

    but i still can't think how i can use it to do my work. e.g. in my case, the key variable / condition is if > 0 or not

     remaining_amount_needed > 0,
  • WarrenBelz Profile Picture
    153,084 Most Valuable Professional on at

    @baron33,

    Maybe set the variable after the ForAll to the value of the field in the last record of the collection

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 739 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard