Skip to main content

Notifications

Power Apps - Building Power Apps
Suggested answer

Odd behavior in Toggle control

Posted on by 130
I have a toggle control that I am using to set an Is Active flag for a record in the database. I have a Power Automate Flow that calls a stored procedure that updates the value in my SQL Server DB. I have set the Default value of the Toggle to be the bit value in the database via a Lookup
 
LookUp('VC.BusinessEntities',BusinessEntityId=galBusinessEntity.Selected.BusinessEntityId).IsActive
This is all working well so far.
 
I have the OnChange property of the toggle set to run my flow:
 
UpdateContext(
    {
        OnChangeResult: VC_BusinessEntityIsActive.Run(
            galBusinessEntity.Selected.BusinessEntityId,
            tglIsActive_CarrierEdit_1.Value,
            User().FullName
        )
    }
);
 If(
        OnChangeResult.statuscode = "200",
        Notify(
            "Save Successful",
            NotificationType.Success,
            5000
        ),
        Notify(
            OnChangeResult.errormessage,
            NotificationType.Error,
            5000
        )
    );
Again, so far so good. The odd behavior is that the first time I access my edit screen where this control lives it fires the flow even though I haven't selected or changed the value of the control. Is this the intended behavior? How do I configure the control so that this doesn't happen?
Categories:
  • jpespena Profile Picture
    jpespena 264 on at
    Odd behavior in Toggle control
    Hi,
     
    If my post helped resolve your issue, please click Accept as Solution—this helps others find it more easily and marks the item as closed. If you found this or my previous reply helpful, a Like would also be appreciated!
  • tjestesjr Profile Picture
    tjestesjr 130 on at
    Odd behavior in Toggle control
    I implemented the If statement as @jpespena suggested and the toggle now behaves as expected.
  • tjestesjr Profile Picture
    tjestesjr 130 on at
    Odd behavior in Toggle control
    I moved the following code from the OnChange property to the  OnCheck and OnUncheck properties. But I get the same result, which is that the flow is run automatically.
    UpdateContext(
        {
            OnChangeResult: VC_BusinessEntityIsActive.Run(
                galBusinessEntity.Selected.BusinessEntityId,
                tglIsActive_CarrierEdit_1.Value,
                User().FullName
            )
        }
    );
     If(
            OnChangeResult.statuscode = "200",
            Notify(
                "Save Successful",
                NotificationType.Success,
                5000
            ),
            Notify(
                OnChangeResult.errormessage,
                NotificationType.Error,
                5000
            )
        );
     
  • Suggested answer
    jpespena Profile Picture
    jpespena 264 on at
    Odd behavior in Toggle control
    Hi,
     
    I've experienced the same where accessing forms from View mode to Edit mode triggers the OnChange property of components like dropdown and toggles. If you want to keep your code in the OnChange property, you can add a validation that check if the value of the toggle is the same as the selected record, for example if you are coming from a gallery to the edit screen:
    If(
        Gallery.Selected.ToggleFieldValue <> Self.Value,
        UpdateContext(
        {
            OnChangeResult: VC_BusinessEntityIsActive.Run(
                galBusinessEntity.Selected.BusinessEntityId,
                tglIsActive_CarrierEdit_1.Value,
                User().FullName
            )
        }
    );
     If(
            OnChangeResult.statuscode = "200",
            Notify(
                "Save Successful",
                NotificationType.Success,
                5000
            ),
            Notify(
                OnChangeResult.errormessage,
                NotificationType.Error,
                5000
            )
        );
    )
    This code looks if the toggle value is not equal to the current value of the selected item in the gallery, then it perform the flow run.
     

    If my post helped resolve your issue, please click Accept as Solution—this helps others find it more easily and marks the item as closed. If you found this or my previous reply helpful, a Like would also be appreciated!

  • ronaldwalcott Profile Picture
    ronaldwalcott 1,026 on at
    Odd behavior in Toggle control
    Does the toggle have a default value?
    Also, you could consider using the new toggle control which has an OnCheck and OnUncheck property.

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

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,487

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 64,014

Leaderboard