So the other day I was playing around with some field manipulation in PowerApps and thought about a use case I saw often with respect to updating fields when code from a button or some other action occurs. Say for instance you wanted to update a hidden field with some information when a user clicked a button or completed some task on the form. Maybe it isn’t a hidden field, but whatever the case may be, you need to be able to update that field. I was surprised to note that it isn’t very intuitive in PowerApps, nor could I find much out there in the forums. I did find some information around doing it with a Text Input field, but nothing specific to a data card.
What is actually misleading is that there is a lot of forum posts out there that state to use the field’s .Text property. This may have been the case at one time as even the PowerApps support forums have entries with accepted answers stating to use the .Text property (Find an example here). Why this is misleading is because the .Text property is meant as a data provider. It isn’t a consumer meaning you can use that field to set the value. If you use a button to update the .text property of a field it is not going to do anything. However, you can gather data just fine when using the .text property as a data source.
Updating a Data Card from a Button
The quick answer is to use a context variable as the source of the Default property of the field. Then when the button is pressed or another action somewhere is activated that you want to use to update that field, you simply update the value of the variable using the UpdateContext function. Here’s how:
- In the OnVisible property of the screen create a context variable and set it to the current value of the field you want to update.
- You will also likely want to immediately want to run a UpdateContext because when editing the field, for some reason the value doesn’t initially take for some reason. This is only an issue if for some reason you want to revert to the previous value
- Next you update the Default property of the field you want to set and point it to the variable you created. Note: Make sure you are doing this to the data value field (text box) of the data card and not the data card itself)
- In my example I am going to use a second field from the SharePoint list to update the title field. This means I am going to have to use the field .text
property this time. Add a button to the app and perform any customization of it you want. - Next update the OnSelect property of the button to update the context variable we created earlier. Do this by adding an UpdateContext call to the property.
UpdateContext({titleValue:DataCardValue2.Text})
That’s it. So let’s test it out:
Comments
Thanks a lot for this post. This was so hard to find. Couldn’t find any documentation anywhere as to how to do this. Do you have a link to the entire documentation of the syntax/commands used in Power Apps. I just can’t find them.
Thanks mate. I was also surprised that such basic concept is not clearly stated elswhere.