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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Patching a value in a ...
Power Apps
Answered

Patching a value in a Sharepoint list with a value from a collection

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi everyone,

 

I got a problem with the Patch-function.

 

What I have:

A collection (ShoppingCart) with multiple items and a quantity for each item. E.g. "Snickers" "5" and "CocaCola" "2".

A sharepoint list (Inventory) containing each item and a column "InStock" which represents the amount of the item in stock. E.g. "Snickers" and "1000".

 

What it needs to do:

Upon clicking a button, it needs to patch (substract) the right "InStock" column of "Inventory" with the amount bought. E.g. for ProductName = "Snickers": InStock: 1000-5

 

My (not working) idea:

 

ForAll(ShoppingCart; Patch(Inventory;LookUp(Inventory;ProductName = ShoppingCart.Artikel); {InStock: InStock - ShoppingCart.Menge.Value}))

 

In my imagination, it checks for every item of the shopping cart, if it matches with an item of the "Inventory" Sharepoint list. In that case, it patches the column "InStock" of the Inventory-list with substracting the amount of the item in the shopping cart ("ShoppingCart.Menge.Value").

 

It does not work at all and I'm sure it has to do with the formula/syntax.

By the way: I'm from germany. For some reason, our syntax has ";" instead of ",".

 

Thank you very much in advance!

Categories:
  • JR-BejeweledOne Profile Picture
    5,836 Moderator on at

    What type of control is ShoppingCart.Menege?

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

    @Anonymous 

    Please consider changing your Formula to the following:

    Patch(Inventory;
     ForAll(ShoppingCart As _cart; 
     With({_item: LookUp(Inventory;ProductName = _cart.Artikel)}; 
     {ID: _item.ID,
     InStock: _item.InStock - _cart.Menge.Value
     }
     )
     )
    )

    This is assuming that your Menge column is actually a record that has a Value column.  If not, adjust that accordingly.

     

    I hope this is helpful for you.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Thank you very much. This basically works, but I get an issue when two or more items of the same type are in the shopping cart (please see screenshot attached).
    In that case, only the first item gets patched to the sharepoint-list.

     

    The details on the right are roughly translated to: 

    "The requested procedure is invalid. Server response: Error at "Inventory": Conflict while saving. Desired changes are in conflict with another user"

     

    I assume the Formula tries to patch the InStock-value of a product simultaneously with different values and this provokes the error?

     

    Screenshot 2021-03-05 105915.png
  • JR-BejeweledOne Profile Picture
    5,836 Moderator on at

    Per Microsoft when using ForAll:  When writing your formula, keep in mind that records can be processed in any order and, when possible, in parallel. The first record of the table may be processed after the last record.

  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @Anonymous 
    Yes, this will be the case if you have multiples of the same product in your scenario.

    This is because your are trying to update the same record twice.  It considers the first to be modified and the second will then fail.

    What needs to be done then, if you have multiple items, is to combine them and perform the action on only one product at a time.

    Patch(Inventory;
     ForAll(
     GroupBy(ShoppingCart,
     "Artikel",
     "_recs"
     ) As _cart; 
     With({_item: LookUp(Inventory;ProductName = _cart.Artikel)}; 
     {ID: _item.ID,
     InStock: _item.InStock - Sum(_cart, _recs.Menge.Value)
     }
     )
     )
    )

    This will group the cart by the products and then use that to sum the quantities in the InStock value to patch.

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Thanky you Randy. I solved it in the meantime with more or less the same idea, but I have to admit that your solution is way more elegant!

    My approach: 

    ClearCollect(
     ShoppingCartGrouped;
     DropColumns(
     AddColumns(
     GroupBy(
     ShoppingCart;
     "Artikel";
     "Menge"
     );
     "MengeTotal";
     Sum(
     Menge;
     Menge
     )
     );
     "Menge"
     )
    )

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 397 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 354

#3
WarrenBelz Profile Picture

WarrenBelz 232 Most Valuable Professional

Last 30 days Overall leaderboard