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 / Patch multiple records...
Power Apps
Unanswered

Patch multiple records on save depending on selected values

(0) ShareShare
ReportReport
Posted on by 29

Hi,

I have a form in which I have 3 fields.

A text value: request

A number: number

And a choice that maps to a Dataverse choice: methods

When the user saves, I want to produce number times selected method records.

so for example, if users save:

request: ABC

number: 2

methods: M1, M2, M3

I want to save

ABC,1,M1

ABC,1,M2

ABC,1,M3

ABC,2,M1

ABC,2,M2

ABC,2,M3

How do I populate records to Dataverse based on the number user enters?

I'm new to power apps and wondering what's the best way to do it? So far I successfully bind the choice to the Combo box and I can save one record at a time. Do I use a collection to save multiple records? Or would you rather use a Flow and a loop?

I'll really appreciate it if someone can tell me the best practice in this case.

Thanks

Ova

Categories:
I have the same question (0)
  • PowerRanger Profile Picture
    3,458 Super User 2024 Season 1 on at

    @powerbug

     

    I would go with this approach: (tried with SharePoint as a DataSource)

    ForAll(
     Sequence(txtNumber.Text) As number,
     ForAll(
     Sequence(CountRows(cmbMethod.SelectedItems)) As methodCounter,
     Patch(
     SampleList,
     Defaults(SampleList),
     {
     Title: txtRequest.Text,
     counter: number.Value,
     method: Last(
     FirstN(
     cmbMethod.SelectedItems,
     methodCounter.Value
     )
     ).Value
     }
     )
     )
    )

    PowerRanger_0-1644421064527.png

     

    @RandyHayes : Is my ForAll this time correct? If not, you have to point me to some ressources. 🙂

     

    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.

     

     

     

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @powerbug

    You stated that you have a Form - is this a generic term or do you have an Edit form that you are working from?  If so, the approach is different.

     

    @PowerRanger - not so much.  You are still performing the expensive Patch in the body of the ForAll table function.  The goal of the ForAll is to produce a table that can then just be passed to the Patch function.

    So the formula would be this (with corrects mentioned later):

    Patch(yourDataSource,
     Ungroup(
     ForAll(Sequence(Value(txtNumber.Text)) As number,
     {Item: 
     ForAll(cmbMethod.SelectedItems As _method, 
     {Title: txtRequest.Text,
     counter: number.Value,
     method: _method
     }
     )
     }
     ),
     "Item"
     )
    )

    What the above does is create a Table based on the number value chosen (the first ForAll).

    This is going to generate a table that has an Item column in it.  That column will then contain a Table (from the ForAll) that iterates the SelectedItems of the combobox.  That ForAll generates the record with the Title, the counter value and the method value for each selected item chosen.

     

    At the end of it all, you will have (based on the example of the number 2 being chosen for count and M1, M2, and M3 begin chosen) a table with 2 rows in it.  Those two rows will have records with a column called Item that will have a table of the real records needed - the Title, the counter and the method.  There will be three records in the Item table column.

     

    Finally, the table result is Ungrouped to remove the Item column and this will then produce a table with 6 rows in it.  Each row of those 6 will have the Title, counter and Method values.  THAT is then passed to the Patch function.  Since there is no primary key provided in the records, Patch will create them as new records.

     

    I hope this is helpful for you.

  • powerbug Profile Picture
    29 on at

    Not an edit form, just a screen and elements (combo box, text inputs etc) on it.

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @powerbug 

    Okay, then the formula I suggested should do what you need.

  • powerbug Profile Picture
    29 on at

    Thanks so much. I'll test and report back

  • PowerRanger Profile Picture
    3,458 Super User 2024 Season 1 on at

    @RandyHayes Thanks for clarification. After I submitted my response I knew it Was wrong an tried exactly what you proposed as a solution. I just got stuck at the Ungroup() Part... I had this table with 2 rows. Next ForAll will be correct 😁.

     

    Thanks 

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @powerbug 

    Sounds good.  You will likely need to modify control names in the formula, and depending on the Items property of your Combobox, may need to alter the record for the patch to be based on that.

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @PowerRanger 

    Yes - the datashaping formulas are an awesome tool on top of the ForAll tables.  They give you a lot of flexibility on creating the tables exactly the way you want.

    Whenever you have that concept of a ForAll with a ForAll, you will immediately need to think of Ungroup as you will want to "normalize" your table to be a flat set of records to send to patch in a table.  It would be highly confused with a table with a column of tables!

    Yes keep at that ForAll!  Once you really grasp it how it was intended, it becomes one of those "ah ha" moments in PowerApps design because you can use it so much to shape and build tables exactly as you want.

     

    Some bonus material - there are many times that you want to AddColumns to a table that are based on looking up a record and then performing either an operation to get a column from the lookup or perform calculations on it.  ForAll becomes an *awesome* replacement for AddColumns.

     

    Ex.

    AddColumns(someData As _source,
     "ATitle", LookUp(someSource, ID=_source.someColumn, Title),
     "AQty", LookUp(someSource, ID=_source.someColumn, Qty),
     "ACost", LookUp(someSource, ID=_source.someColumn, Cost),
     "APrice", LookUp(someSource, ID=_source.someColumn, Cost) * LookUp(someSource, ID=_source.someColumn, Qty)
    )

    This can get unwieldly and causes a Lot of LookUp data actions.  Ideally a With statement would be preferred, but there is no way to place a With in an AddColumns....SO - ForAll to the rescue, and the above becomes: 

    ForAll(someData As _source,
     Patch(_source, 
     With(LookUp(someSource, ID=_source.someColumn),
     {ATitle: Title,
     AQty: Qty,
     ACost: Cost,
     APrice: Cost * Qty
     }
     )
     )
    )​

    This is equivalent to the above, but there is only One lookup operation performed and the formula is much easier to read and maintain!

     

    Anyway - I could go on forever about ForAll uses.  I still am needing to find time to complete my video series on  <spoiler> "ForAll you know!" 😁

     

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 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard