2

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.

jimerb
  • 67
  • 9

2 Answers2

1

You need to manipulate the default value of the toggle control, and you need to ensure that the number control gets set to the correct value when the toggle control is changed in the form.

If you work with list items in a SharePoint custom form, for example, you can add a toggle control to the card for the SharePoint field. Let's call this "FirstToggle". Then set the default of that toggle control to a formula like

ThisItem.myIntegerField = 1

That will return TRUE or FALSE and swing the toggle.

Next, you need to write the state of the toggle back into the number control. The default value for the number control is then

If(FirstToggle.Value,1,0)

If you use a different scenario, different data source, a gallery, etc., the principle is the same: Load the default of the toggle from the value of the stored data with the formula that compares the value with 1. Set the default of the data field by evaluating the state of the toggle.

enter image description here

teylyn
  • 34,374
  • 4
  • 53
  • 73
1

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

SeaDude
  • 3,725
  • 6
  • 31
  • 68
  • I'm learning a lot from both of these answers. Just wanted to pass my thanks along. I'm using a combo of both posts. – jimerb Dec 21 '20 at 23:40
  • Right on! Toggle bonus #3 here... be sure to place toggle logic `OnSelect` rather than `OnChange` as is customary with other types of controls (Dropdowns, Text boxes, etc.). The reason is that Toggles can actually `Change` when a screen loads! This can erroneously execute code. (Learned that one the hard, hard way :) ) – SeaDude Dec 22 '20 at 01:09
  • What do you mean with "long form code shown"? – teylyn Dec 22 '20 at 19:43
  • Becuase the OP is very new to PowerApps, I wrote out the entire code instead of the short form. **Example:** This `If( glrSampleData.Selected.dataValue = 0, false, glrSampleData.Selected.dataValue = 1, true )` could have been written as `If( glrSampleData.Selected.dataValue = 0, false, true`) – SeaDude Dec 24 '20 at 19:08