cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Super User
Super User

Patch problem; worked before now it doesn't

Hi all!

 

I've got an app handling our internal orders. Sometimes goods won't be delivered all in one go, so we need to split an order. This function has been working well before, but now it has stopped (unfortunately just after we've gone live with the app). Below is an  overview of the two screens involved and also a run through of the formulas I (try to) use to achieve this.

 

There are two main tables: Orderhuvud (OrderHead) and NyOrderRad (NewOrderRow) that are linked by an Id in Orderhuvud and OrderhuvudId in NyOrderRad.See the screen dump below for an overview of one of the app's main screens.

 

Patchproblem_1.pngOverview of orders: Orderhuvud on the left and it's children, NyOrderRad, on the right

Pressing the little icon with a plus-sign in the order details section on the left brings us to the second screen where we can choose which records from an order that need to be moved.

 

Patchproblem_2.pngScreen to select which order items to move to another order

This second screen has a gallery that is populated with this formula, and this works well: 

AddColumns(Filter('[dbo].[NyOrderRad]', OrderhuvudId = rSelectedOrder.Id),"DoSplit", false,"Splitamount", Antal)

 

Here's where the problem arises - when you press the check mark to do the split, I use the big formula that I will go through step by step: 

UpdateContext({rNyttOrderHuvud:Patch('[dbo].[Orderhuvud]', Defaults('[dbo].[Orderhuvud]'), {SkapadDatum:Text(Today(),"[$-sv-SE]yyyy-mm-dd"), HanteradAv:rCurrentUser.FullName,Bestallningsdatum:rSelectedOrder.Bestallningsdatum, Ordernummer:rSelectedOrder.Ordernummer,Kommentar:rSelectedOrder.Kommentar,Leverantor:rSelectedOrder.Leverantor,FraktKonto:nFRAKTKONTO})});ForAll(gOrderrader_scrSO.AllItems, If(cbSplit.Value = true && Value(txtAntal2Split.Text) >= Antal, Patch('[dbo].[NyOrderRad]', LookUp('[dbo].[NyOrderRad]', Id = Id),{OrderhuvudId:rNyttOrderHuvud.Id, Restnoterad:1}));If(cbSplit.Value = true && Value(txtAntal2Split.Text) < Antal, Patch('[dbo].[NyOrderRad]', LookUp('[dbo].[NyOrderRad]', Id = Id), {Antal:Antal - Value(txtAntal2Split.Text), Restnoterad:1});Patch('[dbo].[NyOrderRad]', Defaults('[dbo].[NyOrderRad]'), {Bestallare:Bestallare, BestEpost:BestEpost, BestalldKundDatum:BestalldKundDatum, Anvandare:Anvandare,Avdelning_Plats:Avdelning_Plats,Kategori:Kategori,Benamning:Benamning,Antal:Value(txtAntal2Split.Text),OrderhuvudId:rNyttOrderHuvud.Id,Konto:Konto,LevArtNr:LevArtNr, Pris:Pris,Ansvar:Ansvar,Verksamhet:Verksamhet,Projekt:Projekt,Aktivitet:Aktivitet,Objekt:Objekt,Restnoterad:1,Kommentar:Kommentar})))

 

Step 1: Create a new order head and save this record in a local variable, this works well!

UpdateContext(
	{rNyttOrderHuvud:Patch('[dbo].[Orderhuvud]', Defaults('[dbo].[Orderhuvud]'), 
			{SkapadDatum:Text(Today(),"[$-sv-SE]yyyy-mm-dd"), 
			HanteradAv:rCurrentUser.FullName,
			Bestallningsdatum:rSelectedOrder.Bestallningsdatum, 
			Ordernummer:rSelectedOrder.Ordernummer,
			Kommentar:rSelectedOrder.Kommentar,
			Leverantor:rSelectedOrder.Leverantor,
			FraktKonto:nFRAKTKONTO})});

Step 2: Loop through all the items in the gallery

Step 2.1 Check if an item has been checked

Step 2.2 If checked, see if all items of that particular record should be moved. If yes, update the reference to the newly created Order head from step 1.

 

This is where it stops working. The Lookup just returns the very first record in the table, and not the one checked in the gallery, and thus sets the reference for this wrong record. After that it stops the iteration alltogether, or more likely, it does run twice if I check two items, but the Lookup returns the same record both times so it looks like it runs only once.

If(cbSplit.Value = true && Value(txtAntal2Split.Text) >= Antal,
			Patch('[dbo].[NyOrderRad]', Lookup('[dbo].[NyOrderRad]', Id = Id), 
				{OrderhuvudId:rNyttOrderHuvud.Id, 
				Restnoterad:1}));

I tried replacing the Patch with UpdateIf, but that changed ALL of the records in NyOrderRad, givning me a minor heart failure in the process. Luckily I had a local copy od the table's content that I could restore.

 

How do I trace or catch the error? Since it's in a ForAll I cannot use UpdateContext to catch the result of the Patch. I guess the main problem is in the Lookup, but how woill I get it right?

 

Step 2.3 An item has been checked, but not all items should be moved, see screen dump 3

Patchproblem_3.pngTwo records selected, but not all items should be moved

 Here's the fomula for that part:

If(cbSplit.Value = true && Value(txtAntal2Split.Text) < Antal, 
			Patch('[dbo].[NyOrderRad]', LookUp('[dbo].[NyOrderRad]', Id = Id), 
				{Antal:Antal - Value(txtAntal2Split.Text), 
				Restnoterad:1});
			Patch('[dbo].[NyOrderRad]', Defaults('[dbo].[NyOrderRad]'), 
				{Bestallare:Bestallare, 
				BestEpost:BestEpost, 
				BestalldKundDatum:BestalldKundDatum, Anvandare:Anvandare,
				Avdelning_Plats:Avdelning_Plats,
				Kategori:Kategori,
				Benamning:Benamning,
				Antal:Value(txtAntal2Split.Text),
				OrderhuvudId:rNyttOrderHuvud.Id,
				Konto:Konto,
				LevArtNr:LevArtNr, 
				Pris:Pris,
				Ansvar:Ansvar,
				Verksamhet:Verksamhet,
				Projekt:Projekt,
				Aktivitet:Aktivitet,
				Objekt:Objekt,
				Restnoterad:1,
				Kommentar:Kommentar})))

 

The first call to Patch, in italics above, does not behave as in Step 2.2; seemingly nothing happens, the amount is not updated. Isn't this odd? Almost exactly the same code behaves in two different ways, the difference being the setting of the column values.

The second Patch of this code works well, I do get two new records in the NyOrderrad table with the correct amounts.

 

As I write in the begining, this has worked before, but I'm not sure when it stopped.  I think pretty recently, but that's just a hunch.

 

I hope someone can shed some light on this before I pull my hair out! 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Patch problem; worked before now it doesn't

After a few failed attempts I finally found a workaround!

 

As I suspected, it's the Lookup inside the Patch that goes awry: PA can't differentiate between the Id in the table and the Id in the gallery so it ends up using the Id from the table on both sides! I tried using the @-operator on the right side of the equal sign, but never got it to work.

 

My solution is to use AddColumn and make an alias for the Id-column so the Lookup can refer to the alias. Here are the two revised formulas (the new additions are in boldface):

Gallery:Items

AddColumns(Filter('[dbo].[NyOrderRad]', OrderhuvudId = rSelectedOrder.Id),"DoSplit", false,"Splitamount", Antal, "RadId", Id)

 

Part of the big formula inside the ForAll.

Patch('[dbo].[NyOrderRad]', LookUp('[dbo].[NyOrderRad]', Id = RadId),{OrderhuvudId:rNyttOrderHuvud.Id, Restnoterad:1}))

The questions is still if there is a better way to access the underlying record from the gallery; having to search for the record when it's right there seems a bit of a stupid thing to do. If there is, there would be no need to do the Lookup, and probably saving quite some time, especially if there are many items in the gallery. Luckily the operations in this part of the app don't involve many records, usually only a handful or two at most.

 

Hope someone clever can answer that questions! Hopefully it's something really simple that I have overlooked.

 

 

View solution in original post

2 REPLIES 2
Highlighted
Leo09
Level 8

Re: Patch problem; worked before now it doesn't

Hmmm, another problem that might caused by the new update. I wonder why PowerApps patches always break things.

Super User
Super User

Re: Patch problem; worked before now it doesn't

After a few failed attempts I finally found a workaround!

 

As I suspected, it's the Lookup inside the Patch that goes awry: PA can't differentiate between the Id in the table and the Id in the gallery so it ends up using the Id from the table on both sides! I tried using the @-operator on the right side of the equal sign, but never got it to work.

 

My solution is to use AddColumn and make an alias for the Id-column so the Lookup can refer to the alias. Here are the two revised formulas (the new additions are in boldface):

Gallery:Items

AddColumns(Filter('[dbo].[NyOrderRad]', OrderhuvudId = rSelectedOrder.Id),"DoSplit", false,"Splitamount", Antal, "RadId", Id)

 

Part of the big formula inside the ForAll.

Patch('[dbo].[NyOrderRad]', LookUp('[dbo].[NyOrderRad]', Id = RadId),{OrderhuvudId:rNyttOrderHuvud.Id, Restnoterad:1}))

The questions is still if there is a better way to access the underlying record from the gallery; having to search for the record when it's right there seems a bit of a stupid thing to do. If there is, there would be no need to do the Lookup, and probably saving quite some time, especially if there are many items in the gallery. Luckily the operations in this part of the app don't involve many records, usually only a handful or two at most.

 

Hope someone clever can answer that questions! Hopefully it's something really simple that I have overlooked.

 

 

View solution in original post

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors
Users Online
Currently online: 429 members 6,070 guests
Please welcome our newest community members: