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 / How do I iteration on ...
Power Apps
Answered

How do I iteration on Collection?

(0) ShareShare
ReportReport
Posted on by 227

Hi,

 

       I need to use "For, Do..While, While" base on requirement. But i do not see any formula or function for my requirement. how may i loop on collection?

 

Regards,

Yukon

Categories:
I have the same question (0)
  • Steelman70 Profile Picture
    873 on at

    Hi, can you please provide a little more detail on what you need to do? There are a few alternatives depending on the situation.

  • yukonn Profile Picture
    227 on at

    Hi,

     

            Thank for your reply.

            Let say, i have one collection and we call it collSalesLine. The collection structure is Table{DocNo: xxx,DocLineNo: xxxx,ItemNo:xxx,Qty:xxx,UoM:xxxx}. The data will be save collSalesLine when user select the item and press the button (Add to Card). I want to pass those sales lines inside of collSalesLine to my web service (OData) when the user press confirm button.

     

    PSEUDOCODE

    WHILE i < CountRow(collSalesLine)

          IF FOUND THEN

               DO UPDATE

          ELSE

               DO INSERT

    ENDWHILE

     

    Regards,

    Yukon

  • Steelman70 Profile Picture
    873 on at

    Thanks for clarifying.  I can suggest that with the confim button you delete all matching web service records and then insert all records from the collSalesLine.

    If this could work, I would suggest the following pseudocode for the OnSelect property of the confirm button:

     

    ClearCollect(collSalesLineFOUND, Filter(collSalesLine, FOUND)); Remove (OData, collSalesLineFOUND, All); Collect (OData, collSalesLine)

     

    Where FOUND is the expression where you identify the records that are already in the OData.

     

    Please let me know if this could work or if there are other issues to be resolved or if you need more details.  In particular if you necessarily need to update the existing records instead of removing and inserting (collecting) new ones as I suggest above.

     

     

  • Steelman70 Profile Picture
    873 on at

    PS This could also work but would be more difficult to debug:

     

    Remove (OData, Filter(collSalesLine, FOUND), All); Collect (OData, collSalesLine)

  • yukonn Profile Picture
    227 on at

    Hi,

     

            Thank for your suggestion. You mention to use Collect (OData, collSalesLine) for update back to OData. I may not sure my OData is working with this way. Because My odata Customer API must be pass the value as parameter.

     

    WHILE i < CountRows(collSalesLine)

          CustAPI.ODataPostSalesLine({$DocNo: collSalesLine.DocNo,$DocLineNo: collSalesLine.DocLineNo,$ItemNo: collSalesLine.ItemNo,$Quantity: collSalesLines.Qty,$UoM: collSalesLines.UoM})

    ENDWHILE

     

    Add Infor for loading OData to collSalesLine

          Collect(collSalesLine,CustAPI.ODataGetSalesLine().value);

     

    Please share to me if you have another idea.

    I'll test as you suggest method as well.

     

    Regards,

    Yukon

  • Verified answer
    Steelman70 Profile Picture
    873 on at

    Understood. Here is what I would do:
    1) Check if the custom connection supports bulk operations (i.e. write/delete multiple records)
    2) If not, check if you could use one of the standard connections instead of a custom API
    3) If not, see if you could change your user interface to perform operations with the Add to card button, saving the previous version in a collection. Then you can change this same button's text to Remove from card and return the old version if selected
    4) If all of the above do not work then there is this hack: use a timer control. You can set context variable i to 1 and start the timer on pressing the confirm button, do your update/insert operation on timer start then update a context variable i = i+1 on timer end and have the timer repeat while i <= countrow. Set the timer duration to as low a value as will work. You can even hide the timer control.

    This last suggestion is a bit messy, which is why it is not first, but I have made it work in a similar situation. Please let me know if you get stuck.

  • yukonn Profile Picture
    227 on at

    Hi,

     

    1) Check if the custom connection supports bulk operations (i.e. write/delete multiple records)

      - Not support for multiple records Smiley Sad


    2) If not, check if you could use one of the standard connections instead of a custom API

      - does not exist std. connection. may be coming soon but not sure.


    4) If all of the above do not work then there is this hack: use a timer control. You can set context variable i to 1 and start the timer on pressing the confirm button, do your update/insert operation on timer start then update a context variable i = i+1 on timer end and have the timer repeat while i <= countrow. Set the timer duration to as low a value as will work. You can even hide the timer control.

       - I need to use this method. Personally i don't like this method.

     

    Thank for your best suggestion. I'll request to add Iteration to MS PowersApp Team.

    I need to try 1st method.

     

    Regards,

    Yukon

  • hpkeong Profile Picture
    2,944 on at

    Hi

     

    Just saw your thread.

    I have shared Autosaving method using iteration.

    Just sharing in case you need it.

     

     Capture.PNG

     

  • yukonn Profile Picture
    227 on at

    Hi,

     

            Thanks for sharing. If i have any issue, i'll let you know again.

     

    Regards,

     

  • NPrice99 Profile Picture
    1,302 on at

    Thanks - That works Great @Steelman70 

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