web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How to set a variable ...
Power Apps
Answered

How to set a variable based on field values during OnEdit event

(0) ShareShare
ReportReport
Posted on by 85

I'm customizing a SharePoint edit form using Power Apps. I want to set the value of a global variable when the user edits a list item. The variable value will depend on a field value in the item being edited. 

 

I've tried using the following code within the SharePointIntegration OnEdit event:

If(SharePointIntegration.Selected.'Status'.Value = "Active",
  Set(varNextStatus,"Option 1"),
  Set(varNextStatus,"Option 2")
);
EditForm(SharePointForm1);

 

This doesn't work consistently. It appears that sometimes it is referencing the previous record edited and sometimes the current record. 

 

I've tried moving the EditForm command before the If statement, but I see the same behavior. 

 

I found some posts that suggested calling the Refresh function. This generates an error, saying its not supported in this scenario. 

 

Categories:
  • LaurensM Profile Picture
    12,516 Moderator on at

    Hi @ivan_wilson,

     

    I did some testing on my end and came accross the following issue/behaviour:

    • The variable receives a value when I open the Form & press edit
    • When I exit out the app, that value is still saved and will remain that value until I open the next item and click on edit again.
    • Issue: When opening the next record and not selecting edit, the variable still has the previous value saved.

     

    A way of avoiding this behaviour is by adding Set(varNextStatus, Blank()) to the OnView property of the SharePointIntegration. This way, your variable will be reset when the Customized Form is opened and will only receive a value once the user clicks edit.

     

    If this solves your question, would you be so kind as to accept it as a solution & give it a thumbs up.

    Thanks!

  • Ivan_Wilson Profile Picture
    85 on at

    Thank @LaurensM, I added the Set(varNextStatus, Blank()) to the OnView property, but I still have the same issue. It may be due to how my app is configured. I only have one screen with one EditForm. 

     

    I did the same test as you and everything works fine if I open each item in view mode and then click the Edit All button. However if I use the Edit option from the item's context menu, the odd behaviour returns. 

  • Verified answer
    LaurensM Profile Picture
    12,516 Moderator on at

    Hi @ivan_wilson,

     

    Thank you for the feedback! I also just experienced that behaviour, it seems that the Integration.SelectedItem sometimes has some sort of delay. What solved the issue in my case, was no longer referencing the Integration but rather the DataCardValue (Status) directly.

     

    If you display the text in a separate label:

     

    //Text property
    If(
     //Change to correct DataCardName
     //if it is within the Form you could also reference ThisItem.Status.Value
     DataCardValue.Selected.Value = "Active",
     "Option 1",
     "Option 2"
    );
    
    //Visible property
    Form1.Mode = FormMode.Edit

     

    If you use the options as a default selection in a dropdown / TextInput:

     

    //Default property
    If(
     Form1.Mode = FormMode.Edit,
     If(
     //Change to correct DataCardName
     //Since it is within the Form you could also reference ThisItem.Status.Value
     DataCardValue.Selected.Value = "Active", 
     "Option 1",
     "Option 2"
     )
     //You could add your view/new form default here (add a comma after the bracket above)
    );
    

     

    If this solves your question, would you be so kind as to accept it as a solution & give it a thumbs up.

    Thanks!

  • Ivan_Wilson Profile Picture
    85 on at
    Thanks for your persistence @LaurensM. I think I have it working now. I needed to change the approach slightly because I also have buttons on the form that need to change the status.

    From what I can tell, you can't directly update the value of a combo-box control, so I use the variable varNextStatus to store the status I want to set on save.

    I then set the Request Status data card's Update property to {Value: varNextStatus}. 

    In the combo-box control for this data card, I use an If statement in the DefaultSelected property to initialize the property:

    If(
        ThisItem.'Request Status'.Value = "Active",
        {Value: "Draft"},
        Parent.Default
    )

    The OnChange event for the combo-box control updates the varNextStatus variable. This ensures the list item's property will be correctly set on submit.

        Set(varNextStatus,Self.Selected.Value)

    Finally, the buttons that I'm using to save and close the item with a new status uses the following code:
        Set(varNextStatus,"New Status");
        SubmitForm(SharePointForm1);

    I noticed that a common approach that others use to modify a choice property is to use a Patch command. I want to avoid this approach as it could impact on Power Automate flows that are triggered on item updates. I'm not sure if this is a valid concern or not.

    With the above approach, I've been able to remove code that I was running in the SharePointIntegration events (OnEdit, OnSave, etc)

    Thanks for your help in figuring this out @LaurensM. I hope this helps others who are trying to do something similar.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 360 Super User 2026 Season 2

#2
M Bilal Khan Profile Picture

M Bilal Khan 325

#3
Mohsin Ali Profile Picture

Mohsin Ali 232

Last 30 days Overall leaderboard