UPDATE: this post is quite old. I still thing custom functions are not as portable as they should be. However in any complex solution I use the custom functions. The ones I use are from modularfilemaker.org
Whats the best way to pass multiple parameters to a script? Many people use custom functions to package up name value pairs into a text string, and another custom function to parse them out. Other people write name value pairs in the form of a Let Function so they can be easily turned into $script variables in the script being called. I would like to argue for much simpler approach that employs pure built in FileMaker functions, and is very easy to use.First, lets discus Custom Functions. Custom Functions have their place. There are times when nothing else will do. But for something that is so basic and fundamental to good system design as passing multiple script parameters to a script, they are more trouble then they are worth.
Custom functions are not as portable as they should be. If you don’t have FileMakwer Advanced, you can’t put them into your file. There is no way to automatically install them. This makes your code fragile. It will break when it is moved into a new file that doesn’t have the custom function. Also lots of people insists on using their own custom functions for passing parameters. These different implementations are often compatible with one another. This makes it harder to share code with other developers.
Another common technique is to pass parameters in the form of a Let Function Variable Declaration. This look something like this“$firstName=”Todd”; “$lastName = “Geist””
Then the script being called can wrap that string in the rest of the Let function and use Evaluate() to declare the variables as $Variables in the script being called. This is better than using custom functions. Your code is more robust because there are no dependancies that may or may not be in place.
But constructing these strings is pretty difficult there is a lot of escaping that has to go on to get everything quoted correctly. This can be improved somewhat by using other code to take care of adding the dollar signs and the quoting later just before you use Evaluate. Then you can write the multi-parameter string like this:“firstName=Todd; lastName= Geist”
This might be easier to write, but we have just introduced a dependency. I have to have code in my solution in a custom function, script, or just calculation snippet that can correctly handle this string. So I don’t think I am better off than using custom functions.
Another thing to consider is that probably 50 percent of the time you are only passing one single parameter to a Script. I don’t think it makes much sense to have to write that in the form of a name value pair. Its overkill.
So what do we do? How do we handle this with the least amount of fuss?
I think the easiest thing is to pass multiple parameters using the List f() Function, and then use the GetValue() function in the calling script to parse the parameters out into discrete variables in the script being called. That looks like this:List( “Todd”; “Geist”)
or if you preferList( “Todd”; “Geist” )
Then the script being called I can just do this.Set Variable $firstName = GetValue( Get(ScriptParameter ); 1 ); Set Variable $lastName = GetValue( Get(ScriptParameter ); 2 );
This is simple to write and simple to parse. It is easy to read and easy to understand. It has no dependancies, and therefor will not break as you pass it around from file to file.
It does have some downsides, although I don’t think they are significant. One is that sometime you have to pass empty parameters because the order matters. If you leave the first parameter out, then the second one becomes the first, and so on. But this is how all built in FileMaker functions work, so I don’t see this as a big deal. The only other case where using something like Name Value pairs might make some sense is when you need to pass a large number of parameters, many of which are optional.
So the next time you-rethink your Multiple Parameter strategy, I hope you will consider using the simple, native functions that Filemaker provides rather than introducing unnecessary complexity and fragility into your code. You will help make the FileMaker world a friendlier place :>)
(See MultipleParameters.fp7 for example scripts)
ARVE Error: need id and provider