Site icon @WonderLaura

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
    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)
      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:

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.

 

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

 

 


Exit mobile version