Passing Script Parameters

This is a quick "how-to" for people new to scripting and script parameters.

Scripts are easily called from menus, buttons and script triggers. When we call a script FileMaker allows us to pass information into the script using the Script Parameter input field.

Passing a script parameter allows you to do many different things. In general, it allows you to write less code and more generic code. The first benefit of this is that you write less scripts. You don't need to write a new script for every single thing. Instead you write one script and pass the unique thing into the script using a parameter. The ultimate benefit of this is that your code is simpler, more reliable, and only in one spot ( not spread through a dozen scripts ).

Notice that the script parameter label ( in English ) says "parameter", that is, a single parameter, not "parameters". This is a feature :neutral_face: that will get in your way at some point in time.

The script parameter is generated using calculations, even when it is only a simple text string or field value. Fortunately, we can use this to our advantage. At present, I think that the JSON functions provide the easiest way to pass multiple script parameters.

In the following image we can see a calculation which is generating a JSON string. The JSON output is a string of text and FileMaker sees this string of text as one parameter. However, the JSON functions will recognise two named items.

Inside your script you can access your script parameter using the Get(ScriptParameter) function. The next image shows one example for retrieving parameters. It uses JSON functions to set local variables. These local variables can then be used throughout the script.

Here we use the two parameters for different purposes. We passed an ID and a Name. We switch layouts and display a different table. We then search for a record using the ID. When we find the record, we set the field "parameterName" using the "name" that we passed using the second parameter.

Parameters can be used in many ways. You can use the value of parameters to fork code, using IF ( $parameter = X ) ELSE IF ( $parameter = Y ) ELSE END IF. This gives you enormous flexibility as you write your scripts.

The length of script parameters is limited. I can't find a direct reference. If someone can locate it that would be great. The FileMaker 19 Technical limits say that calculation formula are limited to 30,000 characters.

10 Likes

Great stuff thanks. Would like to mention that script parameter length is limited. And if used within Perform Script on Server calls very tight due to its limitation to only 1 million characters. (Same for script results unfortunately)

3 Likes

Super helpful, thank you.
Side question: when specifying calculation, why is your syntax color coded? Mine is all black text, but yours is shades of blue. Is that a plugin or setting or something?

calculation dialog box / or composing tool is normally not color-coded except if plug-in is installed - I bet he uses MonkeyBread FM Plugin.

In script Workspace it is built-in or native FM.

2 Likes

That’s a side effect of MBS plug-in

2 Likes

Thanks I've never used a plugin for Filemaker, I'll try this one. Are there any other "must-have" plugins? Anything I should install on the server or on my machine? Thanks in advance

You could start out with one plug-in. In case it does not cover all requirements, take a second one and so on.

@Torsten Thanks... I'm just wondering what plugins the pros use. I don't even know what exist or where to find them or which will make my life better. Colored code is a GREAT thing for debugging... what other standard things do the pros use that I'm missing?

It really depends on what you need. Each plug-in covers a range of functionalities. The ranges covered by different plug-ins overlap. And each plug-in provides functionality other plug-ins do not have. Check your requirements and compare with plug-in feature lists.
In my solutions the MBS plug-in plays an important role and I am absolutely happy with it. You may collect feedback from peers who use other plug-ins in their solutions in order to get an overview.

4 Likes

Well you should absolutely use BaseElement plugin, which is free.
I couldn't live without it. It's like you're get Filemaker 40 with it

1 Like

I have to say that I do not typically use any plugins. The reason for this is that plugins modify your base environment and effect the behaviour of the product. I don't want my development environment to be too different from the environment it will run in. There is no point developing code that is going to behave differently in a client setting.

2 Likes

If you don't like the MBS enhancements to FileMaker, you can disable them all in preferences dialog.

3 Likes