Skip to content

Updating PowerApps Screens and Forms Programmatically

PowerApps is a powerful form generation platform –but the data-flows-centric model can be more than a bit quirky to use if you’re used to more programmatic approaches to form generation, whether that’s in InfoPath, Microsoft Access, WinForms in Visual Studio, or some other technology. The expectation is that you can assign a value to a control or to a field in the data source and the screen will automatically update to reflect your changes. However, this doesn’t work in PowerApps. Instead, you must make the updates to the record and then reload the record. Let’s look at how this works.

Forms

Forms in PowerApps are the way to connect a part of the screen to a data source explicitly. Forms can be either be a view or an edit form. Strangely, these aren’t different display modes of the same control. Edit forms have different capabilities than display forms, so generally you want to use an edit form if you expect you may ever want to edit some of the data – so that you don’t have to recreate the form.

Forms come with two important design time only properties: DataSource and Item. These properties are design time, because they can only be set in the designer. You cannot – while the form is running – change these values.

DataSource is set to one of the data sources setup for your PowerApp. This defines the structure of the data and helps inform the designer what fields should be available. The second property, Item, is more interesting.

The Item property can be set to the result of a function – such as Lookup() – or it can be set to a variable. Generally, it’s a good idea to set the Item to a variable, so that you can control the behavior of the variable separate from being the result of a static function.

Variables

There are two kinds of variables inside your PowerApp. First, there are global variables, which are established with the Set() function. The second are screen-level context variables, which are either passed into the screen through the parameter of the Navigate() function or are established by use of the UpdateContext() function.

Other than the fact that the variables are updated with Set() for global, and UpdateContext() for context variables, either will function just fine to help us update the screen.

Data Cards

Forms are containers for a set of data cards. These data cards are the templates for the display of each of the individual fields from the data source. The wizard prompts you to select which fields you want to have on your form and the general layout. The importance is that each data card is associated to a field in the data source, so when the data field is updated, the display updates.

The problem is that the data card doesn’t have a property that you can set to update the property programmatically – and get that to display on the page. As a result, you have to update the form’s data item and then let the data flow from the record down to the screen.

Text Fields

Before we continue with the discussion of forms, it’s important to note that the approach that we’re explaining here works just fine with regular text fields which are on the screen – even though they aren’t in a form. If you set a text field’s .Text property to the value of a field in an item, this will only be updated when you update the item. If you attempt to programmatically set the .Text property during runtime, your changes won’t be reflected on the screen. However, if you update the variable that the text field is bound to, it will update the text field for you automatically.

To be specific, you must update the record. Updating the individual property in the variable isn’t enough to cause the data flows to fire and update the text field. So, whether we’re doing a form field or a field we want to display on the screen, we need to update the variable and allow that to cause the data flows to fire to update the screen.

Creating Records

Creating a blank record in a PowerApp can be done by calling NewForm() when you have a form; however, doing this doesn’t allow you to set any starting data. In the case where you want to create records that are specific to the customer, but you don’t want the customer number to be in the form, you’re forced to precreate the record, then display it in the form.

Precreating a Record

To create the record, we’re going to use the Patch() function. This takes the data source, the item, and the updates. The data source is easy. It’s the same data source as we’re using for the form. The item is the potentially challenging part. We need to create an item with all the defaults for our data source. To do that, we call Defaults() with the parameter of our data source. This gets an item with all the defaults for the item. To that we add anything that we need to initialize the record. For instance, let’s call our data source “Sample” and assume we need to initialize a field called “Title” to the current date. The code looks like this:

Patch(Sample, Defaults(‘Sample’), { Title: Text(Now(), “[$-en-US]yyyy-mm-dd hh:mm:ss”)})

The call to the Text() function is just to convert the data/time value from the Now() function into a string. Not that we’re calling Patch() with our data source, our item, and what we want to change in the record. Defaults() is used to get a new item with the right defaults.

After this call, we have a new record in our database.

Selecting the Precreated Record

With the new record, we need to get it to display on the form or as a part of the screen. To do this, we update the variable that we have our form set to. In our example, it happens that Patch() returns our updated record, so we can put that inside of a Set() for a global variable or a UpdateContext() for a context variable. If we were to call our variable SampleItem as a global variable, the code would look like:

Set(SampleItem, Patch(Sample, Defaults(‘Sample’), { Title: Text(Now(), “[$-en-US]yyyy-mm-dd hh:mm:ss”)}))

If the variable SampleItem was a context variable, we’d do:

UpdateContext({SampleItem: Patch(Sample, Defaults(‘Sample’), { Title: Text(Now(), “[$-en-US]yyyy-mm-dd hh:mm:ss”)})})

In both cases, the new record will be reflected on the screen whether the fields are in the form or the text field.

Updating Records

That’s fine for new records, but how do you make an update to an existing record? It’s the same process – except instead of specifying the defaults for the patch, you specify the existing variable. It looks like this:

Set(SampleItem, Patch(Sample, SampleItem, { Title: Text(Now(), “[$-en-US]yyyy-mm-dd hh:mm:ss”)}))

That’s a Wrap

It may not be the way you’d typically think about the process in a more traditional form building tool – but it’s effective.

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this: