I can think of hundreds of times when I found myself pounding my head against the desk because I had to define yet another global field just to pass a simple value from one script to another. Like you, I myself hate to have lots of cruft clogging up my databases. Luckily, FileMaker 7+ gave us the wonderful addition of script parameters to help relieve this plight. Sometimes, even this isn’t enough and that’s where passing multiple parameters takes over.
Whenever a script is chosen in a perform script step, Filemaker shows a box for an optional script parameter. You can type in whatever your programming heart desires or you can click the edit button and take advantage of Filemaker’s many built-in functions to construct a parameter. Many times, when presented with this option you probably find yourself thinking . . . Boy, I wish I had space for a bunch of different parameters. Well with the use of that tiny edit button, a couple of Filemaker’s functions and a little bit of ingenuity this actually ends up being doable. To pass multiple parameters in filemaker we are basically going to make a Â¶ delimited list and then crowbar individual parameters out using the
Ok, let’s say we are trying to create a new line item for an order we are going to fulfill for 3 iPhones at a price of $499. We don’t want to have to put the part number (iPhone), the description (“The coolest phone I have ever seen. Wow, it’s also really cheap!”) the quantity(3) and the price($499) in a bunch of different globals to pass our script, so we will just send them in using the script parameter. To do this, select the perform script step and pick the script you are going to call. Then hit the edit button next to the “Optional Script Parameter” box. Filemaker’s trusty old calculation window will pop up and you’re ready to roll. All you have to do is make the parameters you would like to pass into a list like so:
"iPhone" & Â¶ & "The coolest phone I have ever seen. Wow, it's also really cheap!" & Â¶ & 3 & Â¶ & 499
Hit Ok and you are done with your parameter passing. Now comes the part where you pop the values you want from the script parameter. To do this we are just going to simply use the
MiddleValues() function. The complete signature of
MiddleValues ( text ; startingValue ; numberOfValues )
Note: Thanks to David Richards (from the comments) and Genx (from FMForums.com for pointing out that you can simplify these calcs using
MiddleValues. We needed FileMaker 7 support for this particular setup, but if you can live with 8+ only, they’re absolutely right.
GetValuedoes not include the trailing Â¶ so you can avoid all the mumbo jumbo about cleaning that up.
The sole purpose of this function is taking a list of Â¶ separated values, exactly like we created in the previous step, and select the values you specify from it. For instance, let’s say we are in the script we called in the first step and we want to get the quantity out of the script parameter. All we would need to do is execute the following bit of code:
MiddleValues( Get ( ScriptParameter ) ; 3 ; 1 )
This step tells Filemaker to start from the third value in the script parameter and return one value which is effectively just saying give me the third value in the list. If you want the fourth value, the price, just change the number 3 to a 4. The one will always stay the same because you wouldn’t really want to get more then one value out of the script parameter at a time. It wouldn’t really make sense in a typical script.
We have the value out, we still aren’t quite finished. The
MiddleValues function doesn’t get ride of the annoying Â¶ that we tacked on the end of our parameters so to delete it we have to add the following code:
Let( parameter = MiddleValues( Get ( ScriptParameter ) ; 2 ; 1 ) ; If ( right ( parameter ; 1 ) = Â¶ ; left( parameter; length( parameter ) - 1 ) ; parameter ) )
This code is a little confusing but it is basically saying if we have a Â¶ on the end of our parameter take it off.
Now that you have seen how we plan on getting the values out, you are probably thinking to yourself… What happens if I pass a Â¶ in the middle of one of my parameters. This isn’t exactly out of the realm of possibility and it would clearly break your code. For instance if you changed the description on the iphone to read:
"iPhone" & Â¶ & "The coolest phone I have ever seen.Â¶Wow, it's also really cheap!" & Â¶ & "" & Â¶ & 499
Unfortunately, Filemaker will mush (concatenate for you technical types) everything together and create the following list:
iPhoneÂ¶ The coolest phone I have ever seen.Â¶ Wow, it's also really cheap!Â¶ Â¶ 499
Now, everything has been shifted down one because of that extra Â¶ and we can no longer get the price, quantity nor description that we expect. To take care of this we have to escape the Â¶ so that it turns it into just a symbol rather then turning it into a new line in your text. To implement this change we will have to change how we create our list. I know I lied to you, you weren’t quite done with creating your parameter list. Here is the function you will need:
Name: Parameter Parameters: value Formula: Substitute( value ; "Â¶"; "\Â¶" ) & Â¶
Using our new code we would rewrite our last list to:
parameter( "iPhone" ) & parameter( "The coolest phone I have ever seen.Â¶Wow, it's also really cheap!" ) & parameter( "" ) & parameter( 499 )
Not only does this list look a heck of a lot cooler, but it is more functional and a LOT easier to read. OK, we have a custom function to add parameters to a list, we should probably create one to get all that information out so we don’t have to type that horrible blob of code ever again:
Name: GetParameter Parameters: number Formula: Let( parameterValue = MiddleValues( Get ( ScriptParameter ) ; number ; 1 ) ; substitute( left( parameterValue; length( parameterValue ) - 1 ) ; "\Â¶" ' "Â¶" ) )
Before you go off and pass around parameters all willy nilly, there is one final warning when using this style of parameter passing. To demonstrate it, Let’s go back to our iPhone example. Say that you don’t want to include the quantity just yet, but you still want to send over the price and description. If you tried the following:
Parameter("iPhone") & Parameter("The coolest phone I have ever seen. Wow, it's also really cheap!") & Parameter(499)
The $499 will go into the quantity field because your script that creates the line item thinks that the quantity is the third value, but because the quatity isn’t there the price has shifted up into the third position. To alleviate this problem you just need to make sure that you ALWAYS pass all the parameters to a script, even if one is empty. So your parameter list should look like this:
Parameter("iPhone") & Parameter("The coolest phone I have ever seen. Wow, it's also really cheap!") & Parameter("") & Parameter(499)
Hopefully, many of you find this quite useful and it will help you build more powerful and less cluttered databases. For those who would like a more technically advanced way of passing parameters, where you can actually associate each parameter with a name, such as qty = 2, then check back soon for another article.