The FileMaker Let Function

From Dwayne Wright PMP
Certified FileMaker Developer

WEB: www.dwaynewright.com
EMAIL: info@dwaynewright.com
TWITTER: dwaynewright
YOUTUBE: FileMakerThoughts

Please Note: If you are viewing this page in a news feeder, the images may get munged up a bit or other formatting of the posting may fail. For the best experience, please visit the journal directly by clicking (here)

Let( {[} var1 = expression1 {; var2 = expression2...]} ; calculation)

FMP Version: FileMaker 7 And Higher

Found in the Logical family of functions and can return a text, number, date, time, timestamp or container result. The Let Function allows you to use your own variables within the syntax of the function. It can be used to make complex calculations easier because you can reference just about anything as a variable.

The initial thoughts about the Let function variables is that the information is only persistent within the calculation in which it is used. That is to say a variable you define in one calculation cannot be used in another calculation. Technically, this is untrue and almost an urban myth these days. Developers are using the Let function to define global variables that can be persist for the entire users session of a file.

A Let function has 3 parameters which are the variable name(s), the expression that defines each variable and the calculation that uses the variables to return a result. It tends to look something like...

variable = this expression
variable1 = this other expression
variable2 = this yet another expression
calculated result is (variable + variable2 + variable3)

Another basic example ...
Let(dw=1;dw+dw) would return 2. This is we said that the variable of dw equals 1. Next we calculate dw + dw, which means 1 + 1 when looking at the variable values.

As you may have noticed, I have said variables. In most cases, a working Let function is going to have multiple variables. To help FileMaker keep track of all of the variables within the Let function, you must enclose all your variables within square brackets characters. Each expressed variable must also must be separated by semicolons and don’t forget the semicolon between the last enclosing bracket and the calculated result parameter. So our first example needs to look more like ...

[variable = this expression;
variable1 = this other expression;
variable2 = this yet another expression];
calculated result is (variable + variable2 + variable3

The laws of FileMaker calculation nature are a bit looser within a Let function. You can have a variable with the same name as a previously declared variable. The second instance of a variable name will supersede the previous instance. You can also have variable names that are the same names as fields. Although, I don’t think any seasoned developer would include doing that as a best practice.

You can also use script variables inside of a Let function. Local script variables are only active within the script that calls upon them, as that script executes. So the Let function would probably be used in the calculation engine within one of the many script steps that can call upon the calculation engine.

Finally, I should mention three powerful ways Let functions are quite handy. They can make a complex calculation easier to read. This is because you can set variables equal to calculated values and then calculate them. Otherwise, you have to nest calculations within calculations and that can be hard to keep track of. The Let function can also be much faster than a calculation that does not use the Let function. This is because a variable once declared within a Let function can be used in a calculation repeatedly without a recalculation.

For example, lets say that you have a calculation that uses the Get(FoundCount) four times. In a traditional calculation, every time the calculation needs the Get(FoundCount), it looks at the database and calculates it. So you would be calculating the same result four times. In the Let function, you would calculate it once for the variable and then use the variables result four times without a recalculation taking place.

ANOTHER POST THAT MIGHT HELP: The FileMaker Let And Filter Functions For The Uninitiated

=
More info about the author and FileMaker in general, contact me at info@dwaynewright.com.

© 2009 - Dwayne Wright - dwaynewright.com

The material on this document is offered AS IS. There is NO REPRESENTATION OR WARRANTY, expressed or implied, nor does any other contributor to this document. WARRANTIES OF MERCHANT ABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED. Consequential and incidental damages are expressly excluded. FileMaker Pro is the registered trademark of FileMaker Inc.