• Please enter your DISQUS subdomain in order to use the 'Disqus Comments (for Joomla)' plugin. If you don't have a DISQUS account, register for one here

Core FileMaker Blog

Keep up to date with CoreSolutions

Simple Boolean Toggle

Toggle Switch

I’m going to keep today’s blog post simple and to-the-point.

It’s pretty common in programming to need to toggle between 2 states (boolean). FileMaker Pro however, doesn’t provide a boolean field, nor does it provide a function that will toggle a number field from a 0 to a 1 or vice-versa. Because of this, I’m guessing that most FileMaker developers end up using “If” statements to accomplish this kind of thing. While using an “If” statement in a situation like this isn’t going to be all that expensive (clock-cycle wise), I’d like to encourage developers to think a little outside of the box. There’s almost always more than one way to accomplish what it is you’re trying to do, and toggling boolean values like we’re doing here is certainly no exception.

Here’s one way that this toggle can be handled:

Abs ( $$booleanValue - 1 )

That’s it!
Subtract one from the current value, then take the absolute value of your result.
If your value was a 0, then we’ll subtract one from it… giving us -1. Then we take the absolute value of that, and voila… we’re back to 1.
If your value was 1, we subtract one from that, and we’re down to zero. Taking the absolute value of 0 won’t change anything here.


Leave a comment


  • John
    John 16 December 2016 Report

    I don't quite follow this example.
    //* Toggle Boolean [ fieldName ] --- Set Variable [$fieldName ; Value:Get (ScriptParameter)] Set Variable [$fieldValue; Value:GetField ( $fieldName )] Set Field By Name [$fieldName; Abs ($fieldValue - 1)]

    What part is the code. Can you give another example?

    Comment Link

  • Matthew Leering
    Matthew Leering 16 September 2013 Report

    Yes, absolutely!
    I would say that the 'not' trick is probably the simplest and clearest way to do it.
    I know of some people toggling using XOR as well.
    I always find it interesting to see different approaches to the same problem.

    Comment Link

  • Jeremy
    Jeremy 13 September 2013 Report

    Isn't it simpler to just use this?

    not $booleanValue

    More importantly, I think this better expresses the developer's intent.

    Comment Link

  • Matthew Leering
    Matthew Leering 04 September 2013 Report

    Thanks for sharing this Marc;
    I love this kind of portable/modular code.
    We use scripts like this in our projects quite frequently,
    and even label script parameters in the script name using the same style you did.

    I should note that there is a distinction (albeit subtle) between your script, and my code example though.
    The script that you have provide will toggle the value of a field, whereas the code example I provided will toggle the value of a global variable.

    Comment Link

  • Marc Wood
    Marc Wood 03 September 2013 Report

    Here is a script using your calculation to toggle a boolean value in FileMaker, that can be pasted into any file with no dependencies:

    Toggle Boolean [ fieldName ]
    Set Variable [$fieldName ; Value:Get (ScriptParameter)]
    Set Variable [$fieldValue; Value:GetField ( $fieldName )]
    Set Field By Name [$fieldName; Abs ($fieldValue - 1)]

    Use the GetFieldName function to pass the parameter, and the script won't break if the field name changes in the future.

    Comment Link