Search code examples
powerappspowerapps-formula

Powerapps - Using a Toggle Switch with Integer Data


I'm VERY new to Powerapps.

My data has integer values that are being used for 0 or 1 (no other values.)

The toggle switch wants a binary value.

How can i convert the reading and writing to binary to be able to work with this data type? Looking for some type of convert function.


Solution

  • It depends on whether the Toggle control is inside or outside the Gallery.

    Here is an example of outside:

    1. Set the Toggle control's Default property to:

    (Long-form code shown)

    If(
        glrSampleData.Selected.dataValue = 0, false,
        glrSampleData.Selected.dataValue = 1, true
    )
    

    There are some clean up items you'd want to enable here as well.

    Example:

    • What happens when the Gallery is first initialized?
    • What state should the Toggle be in?
      • Since there isn't a third logical state for this control, I've found it best to just hide it from the user.
      • In this case, the Toggle value will default to false so you'll want to prevent any data collection from being submitted, etc. Unless the value has been explicitly set by the user.

    To hide the Toggle control if the Gallery has no selected record:

    1. Set the OnStart property of the App control to:
    Set(varResetGallery, {});
    Set(varResetGallery, Blank());
    Set(varResetGallery, {})
    
    1. Set the Visible property of the Toggle control to:

    (Again, long-form code shown)

    If(
        IsBlank(glrSampleData.Selected), 
        false, 
        true
    )
    

    Example in action: enter image description here