cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jhn1
Level: Powered On

Onchange event often triggers when screen becomes visible (date picker control)

Greetings PowerAppers,

 

I have a date picker control on an edit form in an app I am finishing up.  The form and the data card are in Edit mode.

There are a few formulas in the control's OnChange event to trap for errors and Patch the date change if no error is found.  Often, when I navigate to that screen, the OnChange event is fired immediately - without me even touching the control!  I get a notification that should only be displayed when a user is trying to change the date.  It doesn't happen every time the form becomes visible.  But, often. There is another date picker control on the same form and it is behaving normally.  

This is very puzzling and I am not sure what to do.  Does anyone have any advice for me?

jhn1

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
jhn1
Level: Powered On

[It's a Bug!] Re: Onchange event often triggers when screen becomes visible (date picker control)

I solved my issue.  It turns out that there is a bug in PowerApps such that simply changing records triggers the OnChange event of my DatePicker control when my gallery’s selection moved to a record with a different date.  In other words, if the StartDate was the same in the newly selected record, OnChange did not get fired.  If the StartDate was different, it did.  The seemingly random behavior was not random at all.  What seemed to be triggering the problem (OnVisible of new screen) was actually the change of gallery selection (different record.)

Once I realized what was happening, I was able to alter my formula such that it only executes when the user attempts to alter the date inside the control.  To do this, I wrapped my long OnChange formula in this If statement:

    If(ThisItem.StartDate <> dateValueStartDate.SelectedDate,
                         [my long formula]
    )

This means: only execute the long formula if the date’s been altered by the user and not yet saved. 

 

-jhn

View solution in original post

6 REPLIES 6
Community Support Team
Community Support Team

Re: Onchange event often triggers when screen becomes visible (date picker control)

Hi @jhn1,

 

Could you please share a bit more about the formula within the OnChange property of the DatePicker control?

 

I have made a test on my side, and don't have the issue that you mentioned. The GIF screenshot as below:Test1.gif

 

 

Set the OnChange property of the DatePicker control to following formula:

Notify("Date is changed",NotificationType.Success)

When the Date is changed, the message would show up.

 

Please take a try to re-create your app, then check if the issue is solved.

 

If the issue still exists, please take a try to delete the DatePicker control from the corresponding Data card, and then re-add a new DatePicker control within it, then check if the issue still exists.

 

More details about deleting the DatePicker control from corresponding Data card and re-add a new one within it, please check the following GIF image: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.
jhn1
Level: Powered On

Re: Onchange event often triggers when screen becomes visible (date picker control)

Thank you, @v-xida-msft for your response.  I tried recreating the datepicker control and had the same issue.  The OnChange event was fired when the screen became visible.  I didn't touch the datepicker control to get this result.

 

Here is the code in my datepicker's OnChange event. 

If(
    CountIf(
        '[dbo].[tblAssignments]',
        AProjectID = galProjects.Selected.ProjectID
    ) = 0,
    If(
        dateValueStartDate.SelectedDate > dateValueEndDate.SelectedDate Or Weekday(
            dateValueStartDate.SelectedDate,
            StartOfWeek.Monday
        ) <> 1,
        Notify(
            "Please check the project's start date for an error. The start date must be before the end date and must be a Monday.",
            NotificationType.Information
        ),
        Patch(
            '[dbo].[tblProjects]',
            galProjects.Selected,
            {StartDate: dateValueStartDate.SelectedDate}
        )
    ),
    Notify(
        "You can't adjust the start date of this project.  Assignments have been scheduled which may conflict with your changes.  To adjust the project start date, please delete all the project's assignments and try again.",
        NotificationType.Information
    ),
    Reset(dateValueStartDate)
)

The notification that is displayed when the screen becomes visible is the one that starts with "You can't adjust the start date of..." Any insights would be appreciated.

 

Thank you!

 

-jhn1

Highlighted
jhn1
Level: Powered On

[It's a Bug!] Re: Onchange event often triggers when screen becomes visible (date picker control)

I solved my issue.  It turns out that there is a bug in PowerApps such that simply changing records triggers the OnChange event of my DatePicker control when my gallery’s selection moved to a record with a different date.  In other words, if the StartDate was the same in the newly selected record, OnChange did not get fired.  If the StartDate was different, it did.  The seemingly random behavior was not random at all.  What seemed to be triggering the problem (OnVisible of new screen) was actually the change of gallery selection (different record.)

Once I realized what was happening, I was able to alter my formula such that it only executes when the user attempts to alter the date inside the control.  To do this, I wrapped my long OnChange formula in this If statement:

    If(ThisItem.StartDate <> dateValueStartDate.SelectedDate,
                         [my long formula]
    )

This means: only execute the long formula if the date’s been altered by the user and not yet saved. 

 

-jhn

View solution in original post

emh
Level 8

Re: [It's a Bug!] Re: Onchange event often triggers when screen becomes visible (date picker control

Thank you jhn,

 

I have encountered the same problem and developed the same solution. I searched for anyone else with this problem so I could validate that it works. So YES, this solution works.

emh
Level 8

Re: [It's a Bug!] Re: Onchange event often triggers when screen becomes visible (date picker control

Adding to the solution, if the date is allowed to be blank then an additional level of if..then is required.

 

 If(!IsBlank(dcvEmailStopDate.SelectedDate),

             If(dcvEmailStopDate.SelectedDate=ThisItem.StopDate, UpdateContext({EmailChange:false}), UpdateContext({EmailChange:true})), If(IsBlank(ThisItem.StopDate), UpdateContext({EmailChange:false}), UpdateContext({EmailChange:true})))

bolin-stratus
Level: Powered On

Re: [It's a Bug!] Re: Onchange event often triggers when screen becomes visible (date picker control)

Thanks @jhn1 for the solution. I have experienced that same issue!

Helpful resources

Announcements
firstImage

Microsoft Business Applications Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

Top Kudoed Authors
Users Online
Currently online: 388 members 3,962 guests
Please welcome our newest community members: