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 function not alw...
Power Apps
Unanswered

Patch function not always updating a value

(0) ShareShare
ReportReport
Posted on by 301

Hello,

I have an issue with a patch function that seems to work sometimes (100% when I test it) but after it is moved from Test to Prod environment and a end user is using the app it doesn't work. 

I can test it with them and it works, but when I go away it doesn't work.  I am totally confused by this behaviour.

 

I have two tables Orders and Containers.

When a container record is being patched as a certain stage in the process, the order the container belongs too needs the quantity field to be updated.

For example an Order needs 4 containers.  When a container is assigned to the order, the Total Fitted field on Order is increased by 1. 

I also want to check if the order now has all the containers it needs and if there are 0 left to fitted the Order status is updated to Fitted.  This also doesn't always happen.

I moved the check if the order has 0 left to do to second screen so that I didn't have 2 functions patching the Order table on the

one button.  But this hasn't helped.

 

On Screen 1 I have the following on a button to save and exit the screen.  See the last patch function.

Patch(QPContainers,LookUp(QPContainers,ContainerID = dpdFCContainer.Selected.ContainerID), {'Bladder No':txtFCBladderNo.Text,'Seal No':txtFCSealNo.Text,'Shipping Line':dpdFCShippingLine.Selected.Value, Location:LookUp('QP Locations','Location Name' = lblFCLocationText.Text), 'Container Status':'QP Container Status'.Located});
ForAll(cltFCCameraPics, 
 Patch(QPContainerPhotos,Defaults(QPContainerPhotos),
 {PhotoName:ThisRecord.name, Photo:ThisRecord.pic,ContainerNo:LookUp(QPContainers,ContainerID = dpdFCContainer.SelectedText.Value),Order:LookUp(QPOrders,'Order Ref No'=lblFCOrder.Text)}
 )
);
Set(varFCNewToFit,lblFCNewToFitQty.Text);
Set(varFCOrderNo, lblFCOrder.Text);
Set(varFCContainerNo,dpdFCContainer.Selected.ContainerID);
Patch(QPOrders,LookUp(QPOrders,'Order Ref No'=lblFCOrder.Text),
 {'Fitted Qty':Value(lblFCNewFittedQty.Text)}
); //To Update the Orders Fitted Qty
If(
 // check if there were any errors when the QPOrders was submitted
 !IsEmpty(Errors(QPOrders)),
 // if true, show any error message
 Notify(
 Concat(Errors(QPOrders), Column&": "&Message),
 NotificationType.Error
 ),
 // else, go to success screen
 UpdateContext({ResetFittingCheckbox:true});UpdateContext({ResetFittingCheckbox:false});

 Navigate(scnFittedQtySuccess);
 )

 

 On screen FittedSuccess I have the following when the button is clicked to save and close the screen.

If(varFCNewToFit in 0,(Patch(QPOrders,LookUp(QPOrders,'Order Ref No' = varFCOrderNo),
 {'Order Status':'QP Order Status'.Fitted}
))); //To update the Orders table order status

 

Any pointers would be appreciated.

Thanks

Rosie

Categories:
I have the same question (0)
  • SoPatt Profile Picture
    Microsoft Employee on at

    One thing I can think of:

     

    ForAll(cltFCCameraPics, 
     Patch(QPContainerPhotos,Defaults(QPContainerPhotos),
     {PhotoName:ThisRecord.name, Photo:ThisRecord.pic,ContainerNo:LookUp(QPContainers,ContainerID = dpdFCContainer.SelectedText.Value),Order:LookUp(QPOrders,'Order Ref No'=lblFCOrder.Text)}
     )
    );

     

    vs.

     

    With({
     // do your lookups once, as opposed to on every iteration within ForAll
     TargetOrder: LookUp(
     QPOrders,
     'Order Ref No'=lblFCOrder.Text
     },
     TargetContainer: LookUp(
     QPContainers,
     ContainerID = dpdFCContainer.SelectedText.Value
     )
    },
     // Update DataSource once with Patch(Collection, Table) 
     // vs many times with ForAll(Patch(Collection,Record,Record)
     Patch(QPContainerPhotos
     ForAll(cltFCCameraPics, // ForAll returns a Table
     Patch( 
     // Patch(Record1,Record2) returns a Record1 "patched" with Record2
     Defaults(QPContainerPhotos),
     {
     PhotoName:ThisRecord.name,
     Photo:ThisRecord.pic,
     Order: TargetOrder,
     ContainerNo: TargetContainer
     }
     )
     )
     )
    );

     

    I think it's more dependable and efficient in terms of network traffic, etc. to do one Patch to your datasource. Review the different overloads of the Patch() function.

    Liberal use of line breaks makes code more comprehensible.

    With statements eliminate redundant code. Also within ForAll, keep in mind that code is going to execute as many times as you have records in the table that you pass as the first argument, so doing whatever you can outside the ForAll makes your code more efficient.

    Also,

    With({
     TargetOrder: LookUp(
     QPOrders,
     'Order Ref No'=lblFCOrder.Text
     )
    ),
     Patch(QPOrders,
     TargetOrder,
     {
     'Fitted Qty':Value(lblFCNewFittedQty.Text)
     }
     )
    )

    However if it's this last Patch that's giving you trouble I am doubtful that this will help.

     

  • Rosie Profile Picture
    301 on at

    @sopatte thanks so much for your pointers. I will definitely make those changes.

    It is the last patch function to update the 'Fitted Qty', it somehow just seems to get missed when the last container is updated.  But there is no error message, it just doesn't update to the new value. 

    It is like it just skips this part.  I guess that is not possible?

  • SoPatt Profile Picture
    Microsoft Employee on at

    Yeah that's very mysterious. The only thing I can think of is a connection issue. You might consider incorporating Connection.Connected where your submit button or whatever is disabled when the user loses connection. But if it's just a momentary thing idk if that will make a difference. Maybe some staffer type around here will have a better idea. I'm a MS employee but I'm not affiliated with the Power Platform team, in case you were wondering. I'm just here as a user and fan of the platform.

    Also are you actually looking at the record in the datasource to validate that the change is not getting made? A Refresh(datasource) might ensure your app is synced. I'm grasping here, along with you I take it.

  • Rosie Profile Picture
    301 on at

    Hi, 

    I have been down the connection road and added in the Connection.connected.

    I am checking the datasource to make sure the change has/has not occurred.

    I found that it seemed to depend on the order I had the statements in.  Not sure that makes sense though.

    I have re-written it as you suggested you With.  Wasn't updating at all after that.

    I had to move the patching of the QPOrders - Fitted qty to be ahead of the patching of the QPContainers - ContainerStatus

    I have just tested it and it has worked but I need to do it multiple times in Dev, then move it to Prod - test multiple times, then get the users to test it too.

    I am using labels like variables (to get the values I need from the record) - my next step is to convert these to Context Variables.

    Here is what it looks like now:

    With(
     {
     CurrentOrder: LookUp(QPOrders,'Order Ref No'=lblFCOrder.Text),
     CurrentContainer: LookUp(QPContainers,ContainerID = dpdFCContainer.SelectedText.Value)
    	},
     Patch(QPContainerPhotos,
     ForAll(cltFCCameraPics,//Forall returns a table
     Patch(QPContainerPhotos, (Defaults(QPContainerPhotos)),
     {
     PhotoName:ThisRecord.name,
     Photo:ThisRecord.pic,
    				ContainerNo: CurrentContainer,
    				Order: CurrentOrder
     }
     )
     )
     )
    );
    //Set variables for using on Success Screen
    Set(
     varFCNewToFit,
     lblFCNewToFitQty.Text
    );
    Set(
     varFCOrderNo,
     lblFCOrder.Text
    );
    Set(
     varFCContainerNo,
     dpdFCContainer.Selected.ContainerID
    );
    
    With(
     {
     CurrentOrder: LookUp(QPOrders,'Order Ref No' = lblFCOrder.Text)
     },
     Patch(QPOrders,CurrentOrder,
     {
     'Fitted Qty':Value(lblFCNewFittedQty.Text)
     }
     )
    );
    
    Patch(
     QPContainers,
     LookUp(
     QPContainers,
     ContainerID = dpdFCContainer.Selected.ContainerID
     ),
     {
     'Bladder No': txtFCBladderNo.Text,
     'Seal No': txtFCSealNo.Text,
     'Shipping Line': dpdFCShippingLine.Selected.Value,
     Location: LookUp(
     'QP Locations',
     'Location Name' = lblFCLocationText.Text
     ),
     'Container Status': 'QP Container Status'.Located
     }
    );
    
    UpdateContext({ResetFittingCheckbox: true});
    UpdateContext({ResetFittingCheckbox: false});
    Navigate(scnFittedQtySuccess)

     Appreciate your help.

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 Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 414

#2
Valantis Profile Picture

Valantis 387

#3
timl Profile Picture

timl 344 Super User 2026 Season 1

Last 30 days Overall leaderboard