How to Add and Update Related Rows In Power Apps - Cloud Limitations

How to Add and Update Related Rows In Power Apps


Power Apps allows you to manage complex data relationships using low-code formulas. This post dives into using Power Fx to streamline your workflow by adding and updating related rows across different tables.

Scenario: Imagine you run an online store and use Power Apps to manage orders and product details. You want a function that:


Creates a new order with a "New" status.

Adds corresponding order details for each item in the order.

Breaking it Down with Power Fx:


Here's the magic behind the scenes:


Explanation:

Patching the Orders Table:


Patch(Orders, Defaults(Orders), {OrderStatus: "New"}): This line updates the OrderStatus field to "New" in the Orders table.
Patch updates a record, and Defaults(Orders) creates a new record with default values for all fields. Here, we only modify OrderStatus.

                        Orders Table

Looping Through Order Details:


ForAll(NewOrderDetails, ...): This loop iterates over each record in the NewOrderDetails table.
Inside the loop, another Patch function creates a new record in the OrderDetails table for each item in the order.
We set the Order field to the current OrderID (likely obtained from the newly created order in step 1), along with Quantity and ProductID from the NewOrderDetails table.
Tables with Sample Data:

                        OrderDetails Table

With a little creativity and these building blocks, you can conquer complex data relationships within your Power Apps!