How to Update a Field through a button's OnSelect Event

Deleted
Not applicable

I've watched quite a few PowerApps videos this weekend and feel I have enough knowledge to try my hand at customizing some forms this coming week. However, something very basic escapes me and I didn't see it addressed in the vids I have watched thus far. 

In InfoPath or visual basic like environments I have used in the past (e.g. Access modules), it was easy to update a field via a button push event (or clicking some button-like graphic). Something like...

 

Status.Value = "Complete"

This does not seem as straightforward in PowerApps. In the form below, I want a quick way to mark a task as done, by clicking on a "button". I would probably replace the Status dropdown with a read-only field or label eventually. 

I figured this formula should work, but nope. What am I doing wrong.

screen.png 

 

 

 

 

 

 

 

 

15 Replies

You would need to set the value of the field you want to change to a variable and then set that variable with the OnSelect event.

 

Depending if you need this across multiple pages you can use either a local (context) or global variable.

 

To set a local variable you can use UpdateContext( { ContextVariable1Value1 } ) or like these 2 examples: UpdateContext( { CountNumber: 1 } ) or UpdateContext( { Name: "John"} )

 

To set a global variable you can use SetVariableNameValue ) or like these 2 examples: Set(  CountNumber, 1  ) or Set( Name, "John" )

 

To use show either of the above variable types is as easy as putting the variable name in the text property of the object you want to use.

 

See these pages for more:

https://docs.microsoft.com/en-us/powerapps/functions/function-updatecontext

https://docs.microsoft.com/en-us/powerapps/functions/function-set

I should mention that if you want to use the variable as the value of an input field you can set the Default property of the field to the variable.

Hey Peter
Thanks I will read that tomorrow AM. That would mean, I am assuming that the variable needs to be initialized to the existing value of the status field when the form is loaded.
I will study it and mark this as the solution once I have it working.
Peter
Do I set the Default property of the target field (Status) to the variable in runtime programmatically or do I do it in advance as a formula?

If it’s it advance, then I think I need to capture the field value and put it in the variable on load, so that the field can be populated with that value; then have my button change the variable to the new value (e.g. “Complete”); then the field will reflect the updated value. Is this correct?

You set it in the formula for the field Default.

 

Yes that can work, or you can use an if statement in the formula, if it is on the input field (not the card) it could be if( IsBlank(VariableX), ThisItem.Default, VariableX) that what the default value of the card is the original data from the database and the field gets a different default value based on if the variable has data or not.

Very helpful Peter. It’s late in my time zone. Will try when I get to work.

Hey Peter, 

 

That is throwing an error. I tried both ThisItem.Default and Parent.Default, but the real error seems to be the variable isn't of the type expected. I have set it as a Text variable. I also noticed that the values available to the dropdown (Not Started, Started, and Complete) don't show up either with this. 

 

recordvalue.png

I would need to build a sharepoint list and powerapp to test this, but what I would try is setting the value of the variable to the actual record that the dropdown in looking for, e.g. 

 

Set(varStatus, Lookup(Status, Value = "Started"))

 

Let me know if trying something like this works and if not I will run up a test app for you.

Hey Peter, 

I gave that a try and it appears to be the wrong syntax...tried various versions and recheck field and control names. If you are able to get a working version it would be very appreciated. 

 

Suggested Form:

 

  • Task (Title) Field - Text, Required
  • Description Field - Plain Text, Multi-line
  • Status Field - Choice, Required, "Not Started" (Default), "Started", "Complete"
  • Due Date Field - Date, Required
  • Link Field - Link or Picture set to URL
  • Assignee Field - Name/Group, Required
  • Button - "Mark Completed" Action: Changes Status Field to "Complete"

I tried to attach a template file, but it's not allowed here. PM me if you want me to email it. 

I have a working solution. I don't know if it's the best solution. 

 

The OnSelect code for the button is:

Patch('Store Task Template',
First(Filter('Store Task Template',ID=Value(IDValue.Text))),
{
    Status:{'@odata.type':"#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
    Value:"Complete",
    Id:1
    }
}
)

One thing I like is that it updates the current item whether in edit more or display mode. That will save mouseclicks for the end user who just wants to mark a task as done. 

I would like to know how I can use the Filter to get the current item without having to look up the ID from a control as I did. 

 

I was getting a nasty error on submit with the last solution:  "An entry is required or has an invalid value. Please correct and try again."  If you click on Submit again, the error goes away. 

Others have discovered that this error is caused by having the SharePoint List ID field on the form. I removed it and it went away. However I need to access the ID to use Patch. So I dug and found I can get the ID from the SharePointIntegration Object as the property SelectedListItemID. 

Works and no submit error message. 

Patch('Store Task Template',
First(Filter('Store Task Template',ID=SharePointIntegration.SelectedListItemID)),
{
    Status:{'@odata.type':"#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
    Value:"Complete",
    Id:1
    }
}
)

 

 

Good to see you found a working solution, just an FYI on your code you might be able to replace the First(Filter('Store Task Template',ID=SharePointIntegration.SelectedListItemID)) with Lookup('Store Task Template',ID=SharePointIntegration.SelectedListItemID) which is essentially the same thing but easier to follow.

https://docs.microsoft.com/en-us/powerapps/functions/function-filter-lookup

 

Nice work!

Thanks Peter, 

I tried substituting the Lookup for the Filter and could not resolve the syntax errors. I'm good for now. I'm pretty happy with the progress over a couple days in learning this from scratch. 

On another app, I got the same error:

"An entry is required or has an invalid value. Please correct and try again."

In this case, I didn't have an ID field on the form. In this case, I had a Title field displayed as read-only. It was a required field, but I didn't create new items using this form, so I needed this field to be read-only.  When I flipped it back to editable, the error went away. So my workaround was to change the visible property to hide the field and to display the Text of that field using a Label control. All is well in my form universe again. 

 

Hi, 

How can we change a column value to variable? 

 I have  and dislike columns in my excel. 

 When the user clicks the Like button under the image I want the column value to be increased by 1, same for dislike button. 

Also for each image if the user clicked Like button she/he shouldnt be able to click dislike button and viceversa. 

How can I do this? 

 

Thanks,