While Loops – New Feature in FileMaker 18

While Loops

While the Loop script step has been around since FileMaker 6, and recursive custom functions have been around since FileMaker 7, a native loop function that could be used within a calculation has remained a long awaited feature, until now!

The New ‘While’ Function in FileMaker 18

The ‘While’ function looks a lot like the ‘Let’ function at first, with a couple additional parameters that allow us to set a condition and perform recursive logic before producing a result:

While (
[ initVar1 = value1 ; initVar2 = value2 ; ...] ;
condition ;
[ logicVar1 = expression1 ; logicVar2 = expression2 ; ...] ;
result
)

When to Use It

The ‘While’ function can’t and won’t replace all loops, of course. There is no way to set a field or go to next record within a calculation, and these are often necessary steps within scripted loops. Also, functions are difficult to debug in general. Despite these deficiencies, we still found reasons why the ‘While’ function is useful and even preferable to the Loop script step when possible.

Slimmer/Lighter Scripts

For example, we use a script called ‘Add Record to JSON’ in our fmLog module that can capture an entire record in JSON using eleven script steps:

These eleven lines of code can now be reduced to a single script step of ‘Set Variable’ using the ‘While’ function:

The While Function is Faster

When we put it to the test using the example scripts above, we discovered that the ‘While’ function is faster than the ‘Loop’ script step! For every thousand records it’s approximately one second faster. This is a noticeable difference when working with 10,000+ records, where an extra ten seconds can feel like an eternity.

I know we’ll continue to find use cases for the ‘While’ function to shine, and we are thrilled to have the option when we need it. You can find additional documentation and examples provided by FileMaker here. You’ll even find some great examples of how the ‘While’ function can be nested within a ‘Let’ function for even more functionality!

If you have questions about the While function function, feel free to contact us!


*This article was originally written for AppWorks, which has since joined Direct Impact Solutions. This article is intended for informative purposes only. To the best of our knowledge, this information is accurate as of the date of publication.