Forum Discussion
How to Update a Field through a button's OnSelect Event
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( { ContextVariable1: Value1 } ) or like these 2 examples: UpdateContext( { CountNumber: 1 } ) or UpdateContext( { Name: "John"} )
To set a global variable you can use Set( VariableName, Value ) 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.
- DeletedDec 11, 2017Peter
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?- Peter McDonaldDec 11, 2017Brass Contributor
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.
- DeletedDec 11, 2017
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.