cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jakemorris
Frequent Visitor

Forall patch inside a lookup - Hairline ..fading.. help please :)

Hi all,

 

building a shopping chart app using x3 separate cds tables, one for orders, one for items available and one for stock available.

 

I have worked out how to removed stock from my CDS table as I add stuff into my cart (cart being a collection displayed in a gallery) but in reality, I would not want to remove the stock until the order had been placed, so I am trying to create a Forall patch that takes each item in the shopping cart collection and removes from the stock table.

 

this is how I am removing stock individual a new collection item is added : 


UpdateContext({ toUpdate: LookUp('Available Stocks', 'Item Name' = ThisItem.'SRN Reference') }); Patch('Available Stocks', toUpdate, { 'Stock Available': toUpdate.'Stock Available' - Dropdown1.Selected.Value });

 

Collect(ShoppingBasket, {itemname:Gallery1.Selected.'Item Name', Size:Gallery1.Selected.Size, SRN:Gallery1.Selected.'SRN Reference', PPEtype:Gallery1.Selected.'PPE Type', Img:Gallery1.Selected.Image, QYT:Dropdown1.Selected.Value})

 

my available stock (item name) and SRN Reference match up which is how I return the record, however when I try this using a button outside of the gallery (unable to use thisItem) I am using this :

 

UpdateContext({ toUpdate: LookUp('Available Stocks', 'Item Name' = First(ShoppingBasket.SRN).SRN)}) ;
ForAll(ShoppingBasket, (Patch('Available Stocks', toUpdate, { 'Stock Available': toUpdate.'Stock Available' - 1})));

 

When I use this it only takes the stock off the first item in the shopping cart, not all items..I guess this is something to do with the fact I am using first in my lookup? but cant see another way around this for CDS as if I remove the first I get an error (record returned not text) 

 

any guidance or help would be much appreciated..

thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Eelman
Community Champion
Community Champion

@jakemorris 

You shouldn't need the UpdateContext variable. I think this will do it.

 

 

ForAll(
   ShoppingBasket As sb, 
   Patch('Available Stocks', 
       LookUp('Available Stocks', 'Item Name' = sb.SRN), 
       { 'Stock Available': sb.'Stock Available' - 1}
   )
);

 

 

Note: I've assumed your code works for CDS because I cannot check this as I don't use it.

View solution in original post

3 REPLIES 3
Eelman
Community Champion
Community Champion

@jakemorris 

You shouldn't need the UpdateContext variable. I think this will do it.

 

 

ForAll(
   ShoppingBasket As sb, 
   Patch('Available Stocks', 
       LookUp('Available Stocks', 'Item Name' = sb.SRN), 
       { 'Stock Available': sb.'Stock Available' - 1}
   )
);

 

 

Note: I've assumed your code works for CDS because I cannot check this as I don't use it.

that's perfect, thank you mate.

Eelman
Community Champion
Community Champion

No worries mate

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Users online (5,554)