Power Apps Notify on Field Change

A common requirement in forms and business processes, is for a notification to be automatically sent only if a specific field is changed in a form.  Historically, this has been tricky to accomplish in workflows.  In this post I’ll show you a way to do it in PowerApps.

In this example, I have a project form, and I want to notify the project manager any time the status is changed.  My status field is a choice field in the SharePoint list.  Basically, we’ll create two variables, one with the original field name, and one with the new field name, and we will compare the two.

    1. First of all, go to your form, and take note of what the name of the control is, where your status field is.  Make note of what it is called.  Mine had a really ugly name, so I renamed it to be something useful.  drpProjectStatus
      powerapps-project-status-dropdown
    2. In PowerApps, click to select the screen that your form is on.  In this case, it is called FormScreen1
    3. Go to the OnVisible property.  Write this function:
      Set(varProjectStatus,drpProjectStatus.Selected.Value)
    4. Then, we need a place to store / show this variable, that we will use later.  Add a label to your form.  You can add it anywhere really, it doesn’t matter, because it’s purpose is just for testing so that you can see what’s in there.  You don’t really need it at all for any functionality.  Call it lblOldStatusIn the Text property of this label, type the name of the variable, varProjectStatus
    5. Select the drop-down drpProjectStatus.  Set the OnSelect property to this, so that now basically when someone changes the status or does anything with it, it will write that value to this new variable.
      Set(varNewProjStatus, drpProjectStatus.Selected.Value)
      powerapps-project-status-onselect
      Side note, you’d think that the OnChange property would work for what we’re trying to do, but when you watch the associated video demo, you’ll see where I struggled to get that to work, so OnSelect is what I went with.
    6. Then, we need a place to store / show this variable, that we will use later.  Add another label to your form. You can add it anywhere really, it doesn’t matter, because it’s purpose is just for testing so that you can see what’s in there.  You don’t really need it at all for any functionality.  Call it lblNewStatusIn the Text property of this label, type the name of the variable, varNewProjStatus.
    7. Now you’ll notice that when you open the form, your old status label will show the status, and when you change the status, the new status label will show the new status.  Keep in mind that the OnVisible will only be triggered when you first arrive on that screen, so clicking the preview button in the PowerApps design surface will not trigger that first variable to be populated.
    8. Next, when the form is submitted, we want to compare those two variables, and if they are different, then do something, like send an email or create an item in a change log.  You may or may not have a submit button on your form.  This logic can simply be added to the form’s OnSuccess property, so that this logic will only happen when the form is successfully submitted.  Of course, this part all depends on what you want to happen when the field is changed.  In this case, I created a simple custom list called Change Log, and I’m sending an email to the project manager.

If(
Not(varProjectStatus = varNewProjStatus),
Patch(‘Change Log’,Defaults(‘Change Log’),{Title:”Status was changed to ” & varNewProjStatus});

Office365.SendEmail(
lblProjectMgr.Text,
lblProjectName.Text & ” status was changed”,
“Please go look at your project”,{Importance:”High”})
)

Note that your OnSuccess property may already have some functions in it, out of box.  Here’s my screenshot, and where I added this:

powerapps-project-form-onsuccess

Also, here’s the demo video of this solution.  I did struggle a bit at the beginning when I was trying to get OnChange to work, but it all worked out in the end.

powerapps-youtube-project-status

 

Check out my PowerApps training classes at iwmentor.com here.

 

 

4 comments

  • Hi Laura. What if I want something to happen when there’s a change in a multiple value person field. How would I detect that change?

    • I think I figured this out. I created two variables (varAssignees and varNewAssignees) that both get set to the SelectedItems of the people picker when the screen is first visible. Then, I have an OnChange formula for the people picker that sets varNewAssignees to the new SelectedItems. The last thing I wanted to do was compare the two and, if different (i.e., someone made a change in the Assigned To field), require a Comment field to be filled in. Because both variables were tables (due to potentially multiple people in them), I filtered on each value in one to see if it was NOT in the other and vice-versa. If both returned empty, then tables were the same and comment was not required. Otherwise, comment was required. My formula for the Required property looks like this:

      If(IsEmpty(Filter(varAssignees,Not(Email exactin varNewAssignees.Email))) && IsEmpty(Filter(varNewAssignees,Not(Email exactin varAssignees.Email))),false,true)

      Seems to be working properly.

  • Hi Laura,
    I think i found a rather neat workaround to the problem. I wanted to make a button visible when a user changed the value of a text field. Simple you would have thought – just set the visibility of the button in the onchange event of the text field. But this does not work – at first I though that the onchage event was not firing but discovered that it was; it just isn’t capable of setting the values of other objects including buttons, fields anything in fact.

    The solution was to introduce a simple variable that gets set when the onchange event of the text field fires because a user changes the value.

    Set(V, “Changed”) – this could be any type but this was my first attempt.

    As an intermediate step I created a text field and set its default value to V (i.e. the variable V) just to see if the onchange was firing and it was.

    Finally I set the buttons visibility depending on the status of the variable V

    If(V=”Changed”,true, false)

    Works a treat!

  • So glad this turned up in a Google search! I was struggling with OnChange, too.

Leave a Reply