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!
Solved! Go to Solution.
@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.
What type of control is ShoppingCart.Menege?
@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.
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?
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.
@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.
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"
)
)
User | Count |
---|---|
256 | |
108 | |
97 | |
51 | |
39 |