Switch-On-String returns one value from a set of cases by matching a string. You give it the field to read, a key and a result for each case, and a default for everything else.
Where a chain of nested If expressions grows one level deeper for every outcome you add, a switch holds every outcome at the same level. Adding a case adds a row. That keeps the expression readable at ten outcomes as well as at two, and it lets you see every key at once when you need to check one against your data.
Before you start
You need a form open in the Designer with two fields: the decimal field that will hold the result, and the string field whose value decides the result. You also need the exact strings that field can hold, because case keys match character for character.
The example below sets Tax Percent (taxPercent) from Worked Alone? (workedAlone), returning 0 for Yes, 5 for No, and 2 when neither matches.
Initial calculations run once
Initial calculations run only when a record is created. If the result has to follow later edits to the controlling field, set the Update property to calculation instead.
Build the calculation
Select the decimal field that will hold the result on the form canvas. Its properties open in the Conditions panel on the right.
In the Conditions panel, set Initial to
calculation. The Calc Editor opens straight away, with no separate button to click.Check the Calc Editor's title bar. It reads
taxPercent | Initial Calculation (decimal). TaxPercent is the name of the field, and the type in brackets scopes every expression the left panel offers you, so a decimal calculation is never offered an expression that returns something else.Type
Switch-On-Stringinto the search box in the Available Decimal Expressions panel, then drag the result onto the canvas, where the empty state reads Drag and Drop a Decimal Expression or a Form Field Here. The block arrives with astringoperand holding a nestedconstant, an emptycasessection, and Add An Optional Parameter below it. The recording further down shows this drag.Set the block's
stringoperand to the controlling field. Click the magnifier inside the operand to open the Field Browser, search for the field and click OK. Dragging it in from the Form Fields panel does the same thing, and needs Show All Fields turned on when the field type differs from the calculation type. The operand changes fromconstanttofieldand shows the field name.Click Add under
cases, or the + beside it, to create a case. The case appears with a placeholder key, a pencil icon, a result slot, and a trash icon.Click the pencil icon beside the case's key, type the string the controlling field will hold, then click the checkmark. Keys match exactly, so spelling and capitalisation both matter.
Drag Constant from the left panel into that case's result slot and type the value the case returns. Any decimal expression works here, not only a constant.
Repeat the three steps above for each remaining case.
Click Add An Optional Parameter, choose default, then drag Constant into the
defaultslot and set its value. This is the result when no key matches.Click OK to close the Calc Editor, then save the form.
The drag, in motion
Opening the Initial property, choosing calculation, searching the expression list, dragging Switch-On-String onto the canvas, and adding the first case.
Check it worked
With the form saved, the field's Initial property reads calculation in the Conditions panel. The design canvas does not evaluate calculations, so the field is still blank there. Switch to Preview to see the result: Tax Percent reads 2.00 while Worked Alone? is empty, because no case matches and the default applies.
Initial runs once
Answering Worked Alone? afterwards does not change the result. Initial runs when the field first takes a value and does not run again when the field it reads changes, so Tax Percent stays at 2.00. To recalculate whenever the input changes, put the same expression on the field's Update property.
If no case matches
Always set a default. With one in place, the calculation returns the default value whenever no case key matches the controlling field, so the field always has a defined result.