Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Unanswered

Setting a field value with click of a button.

(0) ShareShare
ReportReport
Posted on by

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?

Categories:
  • kotlonarendra Profile Picture
    3 on at
    Re: Setting a field value with click of a button.

    Hello,
    I tried the above solution, it works only when i create new entries, but for updating existing entries no action is taken when button is clicked.

    kotlonarendra_0-1609424181574.png

     

    kotlonarendra_1-1609424343128.png

     

     

    Could you please let me know, if i am doing any step wrong.

     

    --

    Thanks,

    Naren

  • v-xida-msft Profile Picture
    on at
    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

  • Srautio Profile Picture
    60 on at
    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.

  • PowerAid Profile Picture
    on at
    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.

  • Srautio Profile Picture
    60 on at
    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?

  • PowerAid Profile Picture
    on at
    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)?

  • Srautio Profile Picture
    60 on at
    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

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,702 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,015 Most Valuable Professional

Leaderboard