
With(Match(“PT2H1M39S”, “PT(?:(?
8. Creating a New Order and Associated Details
The With function is excellent for handling database-like structures. In this example, the With function is used to wrap a pair of operations related to creating a new order and adding details to that order.
Here’s what is happening:
🚀 Creating a New Order: The Patch function creates a new order record in the Orders table with the status “New”. The result of this operation is named and then used in the following steps.
🚀 Adding Order Details: Inside the With block, the ForAll function iterates over the NewOrderDetails table. A new record is added to the OrderDetails table for each row in this table.
🚀 Referencing Values: Within the Patch function for the OrderDetails, you are using values from the previous steps:
🚀 OrderID comes from the result of the first Patch, representing the primary key of the newly created order.
🚀 Quantity and ProductID come from the current row in the NewOrderDetails table being processed by ForAll.
With({NewOrder: Patch(Orders, Defaults(Orders), {OrderStatus: “New”})}, ForAll(NewOrderDetails, Patch(OrderDetails, Defaults(OrderDetails), {OrderNumber: NewOrder, Quantity: Quantity, ProductID: ProductID})))
**NOTE**: OrderNumber is a Lookup Column in the Child table NewOrderDetails
9. Comparing Two Records from Different Sources
Compare records from different sources effortlessly:
Here’s what this code does:
🚀 Getting a Specific Order: Inside the With function, the Lookup function finds the order with ID “Order123” in the Orders table. The result is assigned to the variable SpecificOrder.
🚀 Comparing Columns: Next, inside the With block, an If function compares the columns of SpecificOrder with the currently selected item in Gallery1. In this case, it’s comparing the CustomerID, OrderDate, and TotalAmount columns.
🚀 Returning a Result: Depending on the comparison result, the expression returns a string indicating whether the orders are identical.
🚀 You can use whatever function in the place of the true or false.
With({SpecificOrder: Lookup(Orders, OrderID = “Order123”)}, If(SpecificOrder.CustomerID = Gallery1.Selected.CustomerID && SpecificOrder.OrderDate = Gallery1.Selected.OrderDate && SpecificOrder.TotalAmount = Gallery1.Selected.TotalAmount, true, false))
10. Evaluating Process Status and Emailing User
Suppose you have a Processes table that includes information about various ongoing processes, including a Status field and an AssignedTo field containing the email of the responsible user. You want to check a specific process with ProcessID “P123”, evaluate its status, and send an email notification if the status has changed.
Here’s a breakdown of what’s happening:
🚀 Getting Process Record: The Lookup function finds the process ID “P123” record in the Processes table. The result is assigned to the variable ProcessRecord. (This can be a dynamic ProcessID)
🚀 Evaluating Status: The Switch function evaluates the Status field of ProcessRecord. Depending on the value, it triggers one of the corresponding email-sending functions.
🚀 Sending Emails: The Office365Outlook.SendEmail function sends emails to the user specified in the AssignedTo field of ProcessRecord. The email subject and body are constructed based on the process status.
📝 Note: To use the Office365.SendEmail function requires the Office 365 Outlook connector to properly be configured in your PowerApps.
With({ProcessRecord: Lookup(Processes, ProcessID = “P123”)}, Switch(ProcessRecord.Status, “Completed”, Office365.SendEmail(ProcessRecord.AssignedTo, “Process Completed”, Concatenate(“Process “, ProcessRecord.ProcessID, “has been completed”))))
11. Adding Checked Gallery Items to a Collection
Integrate checkbox controls within galleries for interactive features: (this can be other controls like toggle and radio controls)
Here’s what’s happening in this code:
🚀 Filtering Checked Items: Inside the With function, the Filter function creates a collection of items from the gallery where the checkbox control is checked (Checkbox1.Value = true). This collection is assigned to the variable CheckedItems.
🚀 Iterating Through Checked Items: The ForAll function iterates through the CheckedItems collection.
🚀 Adding to Collection: Inside the loop, the Collect function adds each checked item to a collection named CollectedProducts. You can specify the fields you want to include in the collection, such as ProductID, ProductName, and Price.
🚀 This code segment will result in the CollectedProducts collection containing all the records from the gallery that were checked.
With({CheckedItems: Filter(Gallery1.AllItems, Checkbox1.Value = true)}, ForAll(CheckedItems, Collect(CollectedProducts, {ProductID: ProductID, ProductName: ProductName, Price: Price})))
Conclusion
Utilizing the With function in Power Apps is a game-changing function that allows developers to be more adaptable, productive, and well-organized in their app’s Power Fx formula development. Take advantage of the examples in this blog to elevate your coding techniques and revolutionize how you approach Power Apps development.