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 / Patch datasource with ...
Power Apps
Answered

Patch datasource with value from collection

(0) ShareShare
ReportReport
Posted on by 6

Hi,

 

I'm building an app based on the shopping cart tutorial by Shane Young. (https://www.youtube.com/watch?v=Ztn39KuOW0g)

The items in this web store has a limited quantity and I want the number of items available to update dynamically when changing the number of items in the shopping cart.

 

The data is stored in a sharepoint list called "Products"

 

So... I have a label for quantity in stock with the following code for Text: "Quantity: " & ThisItem.qty

I also have a slider for each item set to Max: ThisItem.qty

Then when you add something to the shopping cart (the plus sign) I use the following code for OnSelect: Collect(colShoppingCart; {Title: ThisItem.Title; Manufacturer: ThisItem.Manufacturer; Qty: Slider1.Value});;Patch(Products; ThisItem;{'Qty in stock': ThisItem.'Qty in stock' - Slider1.Value}) - This works well. When adding items to the cart, the value of items in stock updates correctly.

 

Now to my problems. If you remove items from the cart (code for this is: Remove(colShoppingCart; ThisItem) ) they are removed from the collection BUT I also want the value for quantity in stock to change. I need to pull the value for this item from the collection and add it to the value of items in stock for that same item. That is, I need to patch my datasource (Products) with the current value + the value from the collection.

 

So to cut a long story short: Is there a way to add a value from a collection back to its original datasource?

 

Beowulf_Agat_0-1616062350075.png

 

Categories:
  • Verified answer
    v-jefferni Profile Picture
    Microsoft Employee on at

    Hi @Beowulf_Agat ,

     

    Would you like to deduct the amount of item added into the shopping cart collection from total inventory, and restore the total inventory if removing item from the collection?

     

    If so, since you have already leveraged a Patch function on the plus icon to deduct slider value from inventory quantity, it would be easy for you to understand that another Patch function on the trash icon be used to restore the inventory quantity. But first, you will need to add the deducted quantity into the collection as well.

     

    So, please follow the steps below:

    1\ Add a column in the collection to include deducted quantity:

    Collect(colShoppingCart; {Title: ThisItem.Title; Manufacturer: ThisItem.Manufacturer; Qty: Slider1.Value; remQty:'Qty in stock' - Slider1.Value });;Patch(Products; ThisItem;{'Qty in stock': ThisItem.'Qty in stock' - Slider1.Value})

     

    2\ Add a Patch function OnSelect of the trash icon to update data source:

    Remove(colShoppingCart; ThisItem);
    Patch(Products; LookUp(Prodcts, Title = ThisItem.Title);{'Qty in stock': ThisItem.'Qty in stock'+ThisItem.remQty})

     

    But IMO, a better approach could be: adding a column let's say "Quantity Available" to store the quantity that total storage minus the quantity in the shopping cart. Why doing this? This will help keep the inventory clear, not only to warehouse managers but also to App users.

     

    Hope this helps.

     

    Best regards,

    Community Support Team _ Jeffer Ni
    If this post helps, then please consider Accept it as the solution to help the other members find it.

  • Beowulf_Agat Profile Picture
    6 on at

    Hi @v-jefferni ,

     

    Thanks, this worked out well. However I stumbled upon another issue that I am unable to figure out. 

    If a user puts, say 4 items of the same kind in the shopping cart, these are stored in the collection. And if the user adds more of the same item the collection is patched.

     

    My trouble is that in order to patch the collection I have a column in my SP list called 'Qty in cart' so when adding more items of the same type I do (from the gallery using my SP-list): 

     

    Patch(colShoppingCart;LookUp(colShoppingCart;ID = ThisItem.ID);
    {
    QtyCart: ThisItem.'Qty in cart' + Slider1.Value;
    QtyStock: ThisItem.'Qty in stock' - Slider1.Value
    })

     

    Now this works great but since *Quantity in cart" is stored in my SP-list, it will affect everyone using the app, and potentially mess up their quantity.

     

    I would like to store 'Quantity in cart' in my collection instead (or som other way so it only affects a specific user) so that it doesn't mess up my SP-list. 

    Any ideas? Feels like I've tried everything.

  • v-jefferni Profile Picture
    Microsoft Employee on at

    Hi @Beowulf_Agat ,

     

    Actually I would say I prefer your earlier scenario, since it would be telling users the available quantity of a product for them to add into shopping cart, which is Qty in stock minus Qty in cart, in case the total quantity of a product in all shopping carts exceeds the quantity in the stock.

     

    Anyway, you'd better post a new thread with the updated scenario and details so that all other users in this forum would be able to see it and help you.

     

    Best regards,

    Community Support Team _ Jeffer Ni
    If this post helps, then please consider Accept it as the solution to help the other members find it.

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 405 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 368

#3
WarrenBelz Profile Picture

WarrenBelz 244 Most Valuable Professional

Last 30 days Overall leaderboard