SwitchFields property

FormScreen.SwitchFields — { SwitchField }

The switch fields of this form screen as an array.

Accessing all switch fields of a form screen through this property has many uses:

The following sections detail how these scenarios can be realized. Skip to the examples at the bottom for the concise version.

Taking special action when all switch fields are toggled "on"

The NextScreen property of a form screen determines the screen the user is presented with when they attempt to move on to the next screen. If a warning screen should be shown, instead of the regular next screen, when all switch fields of Screen1 have been toggled to their "on" positions, associate this formula with the NextScreen property of the form screen:

IF(AND(Screen1.SwitchFields), WarningScreen)IF(AND(Screen1,SwitchFields); WarningScreen)

When the formula above is associated with the NextScreen property of a form screen, the net effect is that the user is brought to WarningScreen, instead of to the regular next screen, but only if all switch fields have been toggled to their "on" positions.

Enabling a button only if all switch fields are valid

The Enabled property of a button determines if users can interact with the button. If a button should only be enabled if all switch fields of Screen1 are considered valid, associate this formula with the Enabled property of the button:

AND(Screen1.SwitchFields.Valid)AND(Screen1,SwitchFields,Valid)

Above, the Screen1.SwitchFields.ValidScreen1,SwitchFields,Valid formula returns an array of logical values (TRUE or FALSE), where TRUE indicates that a switch field is valid and FALSE indicates that a switch field is invalid. The AND function, when applied to this array, returns TRUE only if all array elements are TRUE. In effect, the button is only enabled if all switch fields of the form screen are valid.

Making the background color red if a switch field is invalid

The BackgroundColor property determines the background color of a screen and all screens that follow that have no explicit background color set. That means that if the background color is set for the first screen of an app, and no other screens have a background color set, the first screen determines the background color of the entire app.

We can make use of this knowledge to make the background of the entire app red, but only if at least one switch field of the form screen Screen1 is invalid. This formula is associated with the BackgroundColor property of the first screen:

IF(OR(NOT(Screen1.SwitchFields.Valid)), Color.Red)IF(OR(NOT(Screen1,SwitchFields,Valid)); Color,Red)

The formula fragment Screen1.SwitchFields.ValidScreen1,SwitchFields,Valid returns a logical array, where TRUE indicates that the corresponding switch field is valid and FALSE indicates that the corresponding switch field is invalid. Applying the NOT function to this array negates every element, meaning that TRUE indicates that a switch field is invalid and FALSE indicates that a switch field is valid. (The ! operator would have had the same effect.)

Then, the OR function is applied to this array. It returns FALSE only if all elements of the array are FALSE. In other words, it returns TRUE if one or several elements are TRUE, meaning that it returns TRUE if one or several switch fields are invalid.

Finally, the IF function is used to return the color red if one or several switch fields are invalid. Otherwise, IF returns a blank value, which has no effect on the background color. The net effect is that the background color of the app is made red if one or several switch fields of Screen1 are invalid.

Including all switch field values of a form screen in an email

The Body property of email report buttons allows the body of an email to be set through a formula. While email report buttons have built-in support for including field values, through the IncludedFields property, building a text string manually to include in the email body allows us more flexibility.

Consider this formula, which should be associated with the Body property of an email report button:

TEXTJOIN(NEWLINE(), FALSE, Screen1.SwitchFields.Label & ": " & Screen1.SwitchFields.Value)TEXTJOIN(NEWLINE(); FALSE; Screen1,SwitchFields,Label & ": " & Screen1,SwitchFields,Value)

Above, the formula fragment Screen1.SwitchFields.LabelScreen1,SwitchFields,Label returns a text array, made up of the labels of the switch fields of Screen1. The formula fragment Screen1.SwitchFields.ValueScreen1,SwitchFields,Value also returns an array, this time made up of the values of the switch fields. Using &, the labels are joined together with the values, separated by a colon.

The resulting text array, where every element consists of a label, followed by a colon and a value, is converted into a single text string using the TEXTJOIN function. Its first parameter, NEWLINE()NEWLINE(), ensures that the array elements are separated from one another using line breaks.

Ranges versus this property

If the form screen Screen1 only consists of the switch fields Field1, Field2 and Field3, these formulas are equivalent:

Screen1.SwitchFieldsScreen1,SwitchFields
Field1:Field3Field1:Field3
{ Field1, Field2, Field3 }{ Field1; Field2; Field3 }

The second formula uses a range to create an array consisting of Field1, Field3 and all items that appear between them, which in this case is only Field2.

The chief advantage of the SwitchFields property, compared to a range, is that there is no need to update formulas when additional switch fields are added to a form screen. If Field4 were to be added to the form screen, the Field1:Field3Field1:Field3 range would have to be changed to Field1:Field4Field1:Field4 everywhere it is used.

By contrast, Screen1.SwitchFieldsScreen1,SwitchFields automatically includes Field4, and any other switch fields that are added.

Filtering switch fields

If you want to process only a subset of the switch fields returned from this property, use the FILTER function. It can base its decision on which switch fields to return on the property values of the switch fields.

This formula only returns visible switch fields:

FILTER(Screen1.SwitchFields, Screen1.SwitchFields.Visible)FILTER(Screen1,SwitchFields; Screen1,SwitchFields,Visible)

Crucially, you can also filter on the names of the switch fields, using standard text functions. This formula only returns switch fields whose names include the text string "Required":

FILTER(Screen1.SwitchFields, CONTAINS(Screen1.SwitchFields.Name, "Required"))FILTER(Screen1,SwitchFields; CONTAINS(Screen1,SwitchFields,Name; "Required"))

If you use a deliberate naming strategy for your switch fields, you can use FILTER in conjunction with this property to ensure that you only process a specific subset of switch fields.

Related properties

Use the SwitchFields property of a form group to access all switch fields of said form group and the SwitchFields property of the app object to access all switch fields of the entire app.

The Fields property returns all fields of a form screen, not just switch fields. The Items property returns all items of a form screen, including fields, buttons, text boxes and named values. Use NumberFields, TextFields, DateTimeFields, NumberDropDownFields and TextDropDownFields to access other kinds of fields.

Examples

AND(Screen1.SwitchFields.Valid)AND(Screen1,SwitchFields,Valid)

Returns TRUE if all switch fields of Screen1 are valid and FALSE otherwise. Screen1.SwitchFields.ValidScreen1,SwitchFields,Valid returns a logical array, where each element reflects whether its corresponding switch field is valid. Finally, the AND function returns TRUE if all elements are TRUE and FALSE otherwise.

TEXTJOIN(NEWLINE(), FALSE, Screen1.SwitchFields.Value)TEXTJOIN(NEWLINE(); FALSE; Screen1,SwitchFields,Value)

Returns all switch field values of a form screen as a text string, where values are separated from one another using line breaks. The formula fragment Screen1.SwitchFields.ValueScreen1,SwitchFields,Value returns an array of values, which the TEXTJOIN function joins together with line breaks.

AND(Screen1.SwitchFields.Value)AND(Screen1,SwitchFields,Value)

Returns TRUE if all switch fields of Screen1 have been toggled to their "on" positions. Screen1.SwitchFields.ValueScreen1,SwitchFields,Value returns a logical array containing the values of all switch fields. The AND functions returns TRUE if all array elements are TRUE and FALSE otherwise.