cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

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

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!

1 ACCEPTED SOLUTION

Accepted Solutions

@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.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

6 REPLIES 6
JR-BejeweledOne
Super User
Super User

What type of control is ShoppingCart.Menege?



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.
RandyHayes
Super User III
Super User III

@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.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
Anonymous
Not applicable

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?

 

JR-BejeweledOne
Super User
Super User

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.



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.

@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.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

Anonymous
Not applicable

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"
    )
)

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

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.

Users online (1,855)