Custom functions are without a doubt one of the most powerful features in FileMaker; we cannot advocate their use strongly enough.
Simply described, custom functions become available in the calculation dialog as additional functions for use within expressions. They are snippets of code that, just as FileMaker’s pre-established functions do, accept parameters and produce output. One example might be
fnCommission (unitPrice; quantity; discount)
This function would then presumably return a dollar amount based on some formula that multiplied unitPrice
and quantity
, subtracted a discount
from the total, and then applied a percentage or some internal factoring to arrive as a sales commission.
Note that as a developer using the function, we need not even know what that formula might be. All we require is, when fed the three parameters in question, that the function return a meaningful and consistent result.
For example, if ever an organization’s commission rates needed to change, the developer could edit a single custom function and all the calculations based on that function would immediately (depending on their storage settings) reflect and make use of the change.
Custom functions allow developers to abstract portions of their code, independent from database schema or scripts, where it’s possible to reuse a particular piece of logic throughout one’s database (or, by recreating the function in other files, across dozens of databases).
Custom functions can also serve as permanent “system variables” that are not subject to session issues as global fields and global variables are. The values within a custom function do not expire at the end of a user’s session, they are consistent across all users of a database, and a developer can change them centrally as needed.
For more detail and explanation on custom functions, see Chapter 14, “Advanced Calculation Techniques,” in our companion book, Special Edition Using FileMaker 8.
Figure 7.1. The Custom Function dialog allows developers to define parameters that then serve as input for an expression that is then written to reference those parameters.
Custom functions work much like calculations, but it’s important to understand the following aspects of custom functions:
For a review of calculation syntax, see Chapter 4 “Working with Calculations Primer.”
Evaluate()
function. The following example illustrates a scenario where a sales commission is referenced:
$myVar
), FileMaker will store values specific to a currently running script. In the case of a global variable ($$myVar
), the value in the variable will be updated and maintained throughout the file.Let()
function:
Custom functions are extremely powerful tools for building abstract units of logic that can then be reused throughout a solution. Indeed, once a custom function has been created (and tested!) it’s easy enough to recreate them in other files and use them throughout all your solutions. We strongly recommend you create a library of tools to refine and reuse over time. To that end, visit the next chapter for a selection of some of our favorites.