What is a custom function?
Custom FileMaker functions are functions defined by the developer that can be reused anywhere in the database file in which they are created. As developers, we can find ourselves evaluating a calculation multiple times, which is a great sign to use a custom function. A great resource for custom functions is Brian Dunning’s website.
Top 5 Custom Functions
I realize that not everyone’s definition of the Top 5 custom functions will be the same. My top five custom functions are those that I use in multiple databases to save time, or simply those that I use often.
- SysPlatform
- RemoveValue
- Age
- Patternize
- IsServer
SysPlatform
Custom Function Definition
Let ( [
system = Get ( SystemPlatform ) ;
platform = Case (
system = 1 ; "Mac" ;
system = -2 ; "Win" ;
system = 3 ; "iOS" ;
system = 4 ; "Web" ;
//defaultResult
"N/A"
)
] ;
platform
)
When dealing with multi-platform solutions, it is imperative to track which platform the end user is on. I don’t know about you, but I tend to forget which numbers pertain to which system platform, hence why I use this custom function frequently. Instead of constantly using Get(SystemPlatform) and memorizing which number belongs to which platform, you can simply create a custom function that returns the platform. Once doing so, your script process is clearer to read and also quicker to implement.
RemoveValue ( listOfValues ; value )
Custom Function Definition
/*
CUSTOM FUNCTION: RemoveValue(listOfValues; value)
by Debi Fuchs of Aptworks Consulting, debi@aptworks.com
Speedily (and with no recursion or cleanup of empty values) remove all instances of a value from a list. (For cleanup try custom function CullNulls)
e.g. RemoveValue("¶9¶2¶4¶8¶8¶¶7¶¶8¶9¶¶"; "8") -> "¶9¶2¶4¶¶7¶¶9¶¶"
LAST MODIFIED: 28-AUG-2018 by Debi Fuchs of Aptworks Consulting
*/
Let(
s = "##*aaa*##"; //very unlikely string
Case(
//Check if unlikely string rears its head
Position( listOfValues; s; 0; 1 );
//If so, simulate an out-of-range error.
Evaluate( "Factorial( -1 )" );
//Check if value itself contains a paragraph return
Position( value; ¶; 0; 1 );
//If so, simulate an out-of-range error.
Evaluate( "Factorial( -1 )" );
// Otherwise, proceed to get rid of nulls
Substitute(
s & listOfValues & s;
[ "¶"; s & ¶ & s ];
[ s & value & s & ¶; "" ];
[ ¶ & s & value & s; "" ];
[ s & value & s; "" ];
[ s; "" ]
)
)
)
FilterValues returns a text result containing only the values that were passed as filterValues, whereas this custom function is quite the opposite. Many times I have found myself needing to remove a specific value from a value list, which is when the RemoveValue custom function comes in handy.
Explanation of Parameters:
listofValues – value list
value – value to remove
Example:
RemoveValue ( “Green”&¶&”Red”&¶&”Red”&¶&”Purple” ; “Red” )
Returns:
Green
Purple
Age(Birthdate)
Custom Function Definition
Year (Get(CurrentDate)) –
Year (BirthDate) –
(
Get (CurrentDate) < Date (
Month (BirthDate);
Day (BirthDate);
Year (Get(CurrentDate))
)
)
I’m sure many of us have found ourselves needing the age of a person in a record and this custom function does this and calculates the age.
Birthdate – date of birth field you will be using to check age
Patternize ( direction ; pattern ; replaceChar ; value ; pad? )
Have you ever found yourself needing a specific format for a phone number or perhaps you have to mask out credit cards and just display the last 4 digits? This custom function allows you to do both. You insert the pattern and it will output the format you desire.
Explanation of Parameters:
direction – LTR (left to right) or RTL (right to left)
pattern – format you desire
replaceChar – Character in that pattern I want replaced with data
value – data that is passed
pad? – 0 – I don’t want to include the rest of the pattern once I run out of data
Example:
Patternize ( “LTR” ; “###-###-####” ; “#” ; “7183233333” ; 0 )
Patternize ( “LTR” ; “###-###-#### x ###” ; “#” ; “7183233333343” ; 0 )
Patternize ( “RTL” ; “xxxx-xxxx-xxxx-####” ; “#” ; “5288105898105340” ; 0 )
718-323-3333
718-323-3333 x 343
xxxx-xxxx-xxxx-5340
IsServer
Custom Function Definition
PatternCount( Get( ApplicationVersion ); "Server" ) >
This function will determine in a boolean manner whether or not a script is running on the FileMaker Server.
The majority of all Filemaker developers have a OnOpen script for their solution. When running scripts on the server, we usually don’t need to run the OnOpen script. To bypass this instead of creating an if statement and writing out the code we can simply call this custom function.
If you’d like to play around with these versatile functions, check out our demo file: