This post covers how to use Collection functions to work with a list of business customers, displayed in a Data Table control, in Microsoft PowerApps. Future posts will refine this example into a real-world final release which integrates SharePoint as the data source and demonstrates how to use Remove, Filter, Lookup, sort records, and implement a variety of other critical requirements.
Initialize the Collection
A collection can be initialized by using the ClearCollect function, and it can be completely emptied by using the Clear function. Together, the apps OnStart function may be set to something like the following:
ClearCollect(customersCollection,{Name: "", Address1: "", Address2: "",City: "", State: "",'Zip Code': ""}); Clear(customersCollection)
That would result in the app loading with the following:
Add Customers to the Collection
To add customers to the collection you have a lot of options for the UI. For this simple demo, I’ll simply create a new screen with the necessary fields and a “Save” button with its OnSelect property configured to add the new customer record using the Collect function.
I also added some functionality to only show the “Save” button when the Name has been specified; that is the only required field.
Using Functions: Not and IsBlank
These functions can be combined to return true, and show the “Save” button only if the customer’s Name value is specified. This formula is applied to the button’s Visible property.
Not(IsBlank(TextNewName.Text))
Using the Collect Function
We have our input fields, and then set the “Save” button’s OnSelect property to invoke the Collect function and return to the main screen.
Important Note: at this time the “Add New” screen is built from a set of individual controls instead of an Edit form in “New” mode. That’s because forms do not fully support the use of collections as a data source. One can specify a collection as a form’s data source but s/he will be unable to add fields using the PowerApps field picker. When this solution is tied to SharePoint in a later post, I will show how to use a form and its OnSuccess method to handle navigation.
For now, I’ve chained together the Collect function and the Navigate function directly on the “Save” button’s OnSelect property.
Collect(customersCollection,{Name: TextNewName.Text, Address1: TextNewAddress1.Text, Address2: TextNewAddress2.Text, City: TextNewCity.Text, State: TextNewState.Text, 'Zip Code': TextNewZip.Text});Navigate(CustomerListScreen)
Form Current State
At this point, we have a good working prototype to build into a functional real-world solution:
Categories: Apps, Microsoft PowerApps, Office 365 and O365
Leave a Reply