Friday, August 05, 2011

FileMaker TIP: Setting or Passing Multiple Variables At Once

There are times when I'm developing a solution that I need to pass multiple parameters to a script - or when I'm in a script, I want to define multiple variables at once rather than having a whole bunch of "Set Variable" steps - you can just use the "Let()" function do all the heavy lifting.

For example - I have a script that takes in a script parameter from a button, checks the current layout and then performs different logic based on the report button that was pressed:


On the button that calls the script I set the script parameter to the report I want:


Now, if I were to change the variables from local variables (that are only valid as long as the script runs) into global variables (are available until the user quits) - I can set all the values as a single script parameter:
Let ( [ $$WhatReport = "Customers" ; $$CurrentLayout = Get ( LayoutName ) ; $$ShowPreview =  0 ] ; "" )
In the above code I'm using the "Let" function to simply define all my global variables and then I can rewrite my script to look like this:


Pretty cool!

Now there are some pros and cons to this approach. On the pro side: I've greatly simplified my script. On the con side - I now have to put that "Let" function on all my buttons (and any other buttons I create) - on every layout that I want to call this script from.

Personally, I'm not a big fan of passing 100 variables from the calling button/trigger/object - unless I absolutely have to. I can't tell you the number of hours I've spent debugging because I've forgotten to set a certain parameter to the right value - plus, I really don't like having a lot of global variables hanging around when I don't need them (you can't set local variables when passing parameters, unfortunately).

Most of the time (there are exceptions, of course) - I try to keep things as simple and encapsulated as possible. So, taking our first script as the example - here's another tip: you can set multiple local variables within your script the same way as we set the global variables in our script parameter.

To do this - we need to execute a script - and because there is no script step to simply "evaluate" or perform this type of action - we need to set a single variable. In my case I'm setting a local variable called "$blah" to this calculation:



Here's the revised script:



I hope this technique will save you some time when you develop your solution!

No comments:

Web Analytics