cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Schuschu22
Regular Visitor

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

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

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

@Schuschu22 

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

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.

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

View solution in original post

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

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Don't miss the call this month on June 16th - 8a PDT

Top Solution Authors
Top Kudoed Authors
Users online (83,785)