cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper II
Helper II

Setting a field value with click of a button.

I have a phone template app connected to a SharePoint list with a single line of text column called Request Status.  I have a button in the Edit view that is suppose to update the Request Status to "New" when clicked.  The button OnSelect forumal is : UpdateContext({RequestStatusVar: "New"})

 

The Default value of my Request Status field is set to RequestStatusVar.  When I test it out for the first time by creating a new form, I click the button which sets the variable to "New" and Request Status is set to "New" as well.  Then when I click on the Add new Item icon from the Display view, it shows that Request Status is set to "New" before I have even clicked the button for this record. 

 

I do not understand why the variable is set to "New" on the new record.  It seems that variable values carry over to new records?  Why is this the case?  What other code would I need to prevent this?

6 REPLIES 6
Highlighted
Helper I
Helper I

Re: Setting a field value with click of a button.

Unless you are Updating the variable anywhere else in the context you are using it, it will remain whatever value you assign it when it was first initialized. In your case the first time you pressed the button.

 

So unless you Update your variable anywhere else in your form submit process it will keep the value of "New".

 

So right now you could replace the default value of Request Status field to "New" and it would behave the same way. 

 

UpdateContext function in Powerapps

 

Highlighted
Helper II
Helper II

Re: Setting a field value with click of a button.


@Srautio wrote:

Unless you are Updating the variable anywhere else in the context you are using it, it will remain whatever value you assign it when it was first initialized. In your case the first time you pressed the button.

 

So unless you Update your variable anywhere else in your form submit process it will keep the value of "New".

 

So right now you could replace the default value of Request Status field to "New" and it would behave the same way. 

 

UpdateContext function in Powerapps

 


Thanks for the link.  I need some clarification on this info:

 

"A context variable holds its value until the app is closed. If you define a context variable and set its value on a particular screen, that information remains intact even if the user switches to a different screen. Once the app is closed, the context variable's value will be lost and must be recreated when the app is loaded again."

 

So is this saying that unless the PowerApp is closed the current value will still hold?  So as long as the app is open, the value will get passed to other records that you open (in the correspondning screen)?

Highlighted
Helper I
Helper I

Re: Setting a field value with click of a button.

So is this saying that unless the PowerApp is closed the current value will still hold?  So as long as the app is open, the value will get passed to other records that you open (in the correspondning screen)?

 

Yes that is how it should work.

 

What was it you were trying to do with your variable?

Highlighted
Helper II
Helper II

Re: Setting a field value with click of a button.


@Srautio wrote:

What was it you were trying to do with your variable?


I am experimenting on how I can set the Request status field with a click of a Button for a particular record. I do not want the value past on to another record and I do not want the value to be lost in the original record. 

 

I have a lot of experience with Infopath and so I am used to values being "pushed".  I know PowerApps values are "pulled". I am trying to wrap my head around these different concepts in PowerApps like variables.

Highlighted
Helper I
Helper I

Re: Setting a field value with click of a button.


@PowerAid wrote:


I am experimenting on how I can set the Request status field with a click of a Button for a particular record. I do not want the value past on to another record and I do not want the value to be lost in the original record. 


Well if you want to clear your ContextVariable in between submissions you could add the following

UpdateContext({RequestStatusVar: ""})

to either the New Record Screens onVisible property or directly on the Submit button OnSelect property.

 

However variables are not tied to a record, ContextVariables are screenSpecific and GlobalVariables 

Set(variableName, Value)

are accessible all throughout the app.

 

Also you can pass variables in between screens as a third optional value in the Navigate() function.

Navigate(Screen, Cover, {var: "variable"})

Navigate()

 

You can allso use ContextVariables as "toggle-able" booleans by setting the onSelect property of say a button to:

UpdateContext({variable: !variable})

Whenever you click said button it will swap to the opposite value. It defaults to false. I use this alot to hide UI elements from the user untill they click a button by having the UI elements' Visible property set to: variable.

Highlighted
Community Support
Community Support

Re: Setting a field value with click of a button.

Hi @PowerAid,

Do you want the RequestStatusVar value not to be passed to another record, and not to be lost in previous record?

I agree with @Srautio's thought almost, the Context variable (created via UpdateContext function) is scoped to a screen, which means that you can't build a formula that refers to a context variable on another screen. If you don't close your app or change its value, it would remain its previous value continuously.

I have made a test on my side, please take a try with the following workaround:10.JPG

 

11.JPG

Set the OnVisible property of your Edit screen (on your side, it is Edit view) to following formula:

UpdateContext({RequestStatusVar:Blank()})   /* Initialize the RequestStatusVar variable when you create a new form */

Set the OnSelect property of the "Update" button to following formula:

UpdateContext({RequestStatusVar: "New"})    /* Update the RequestStatusVar variable when you click the button */

Set the Default property of the TextInput control within the Request Status Data card to following:

If(
    EditForm1.Mode=FormMode.New,
    RequestStatusVar,
    Parent.Default
)

Or 

Set the Default property of the Request Status Data card to following:

If(
    EditForm1.Mode=FormMode.New,
    RequestStatusVar,
    ThisItem.Request_x0020_Status
)

The GIF screenshot as below:Test1.gif

 

Best regards,

Kris

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Watch Now

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (6,299)