Toggling Boolean (or “On/Off”) Indicators

Often, I need to be able to toggle an indicator field, i.e. switch it on or off.  Obviously there are many ways to achieve this, and using “Yes/No” or “On/Off” value lists on a radio button field was one of my favourites in the past.

But it’s more efficient just to have the user click on the field and reverse whatever setting is currently held.  And to achieve this, I started off with a script which said “If the current value is 1, set the field to 0, and vice versa”.  But the next step in efficiency was to use a bit of pre-school maths, combined with the Abs function in Filemaker.  The Abs function returns the absolute value of a number, i.e. disregarding the sign, so that Abs(-1) returns 1.  So in a situation where a field can contain either 1 or 0, subtracting 1 from that value, and then taking the absolute value will always have the effect of toggling it, regardless of the current value held. If it’s currently set to 1, you get 0.  If it’s currently set to zero, you get -1, of which the absolute value is 1.

So the toggling script that I ended up with is a really efficient one-liner, plus a commit.  Pass the script the field name and the current value as parameters, and “Bob’s your uncle”.  (Note the script uses the “GSP” custom function to extract the parameters.)

EDIT – See comments – Jeremy’s suggestion is even better, not even requiring the pre-school arithmetic, and using only one script parameter (or none, although I think I prefer the version which uses a parameter, thereby avoiding the “awkwardness”):

Set Field By Name [GSP(1); not GetField ( GSP(1) )]


2 Comments on “Toggling Boolean (or “On/Off”) Indicators”

  1. Set Field By Name [Get ( ScriptParameter ); not GetField ( Get ( ScriptParameter ) )]

    … or, if you want to eliminate the script parameter:

    Set Field By Name [GetFieldName ( GetField ( Get ( ActiveFieldName ) ) ); not Get ( ActiveFieldContents )]

    The awkward “GetFieldName ( GetField ( … ) )” construct is necessary to get the fully-qualified field name, since Get ( ActiveFieldName ) does not include the table name.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s