Transcription

Hi, this is Michelle at AppWorks and today I’m going to show you how to pass parameters into a script using FileMaker’s native JSON functions. We’ll start with a simple database of tasks and assignees. As you can see, we currently have a list view for both. What we’ll be creating is a master sorting script that will take in, as parameters, the context, which will either be tasks or assignees, as well as the field that we’d like to sort on. I’ve already turned these headers into some buttons, and, as you can see, when we click on them now, nothing happens. 

We’ll go into layout mode, and we can see that the button is calling our master sorting script, but, since no parameters are being passed in, the script doesn’t yet know where we’re calling the script from, or which field we’d like to sort on. We’ll of course be using FIleMaker’s native JSON functions to pass in these parameters as a JSON object. First, let’s take a look at the sort script that we’ve built to see what the script expects to get. Here’s our master sorting script, and, you can see that it expects to receive two variables, the context and the sort field. It’ll first check the context to see if it is task or assignee, and then it will proceed to sort based off of the field specified.

Right now, the variable for context and sort field is empty, so let’s go ahead and start with our buttons on our layouts and pass in both of those variables as JSON parameters. We’ll be using a native JSON function that FileMaker released in version 16, and this will be JSONSetElement. The first thing it asks for is the JSON object that you’d like to set. If you already have a JSON object in your variable, you can specify that there. We’re starting fresh, so we’ll go ahead and use the double quotes. Our next ones are actually going to be our key valuable pairs, or basically what we’ll be parsing into variables in our sorting script. 

To allow us to create multiple key value pairs, we’ll simply wrap them in brackets and separate them with semicolons. So the first value here is going to be the key. I like the key for this JSON object to match the variable that will be receiving it, so i’m going to name it context. And of course we’re on the task list, so the value for this is going to be “Task” and the type is actually the data type, and for this is just a string, so we’ll use jsonstring. For the next line, we’re going to do another key value pair, the key being the sort_field. The value for that is going to be “Task” as well, because we are sorting on the task field. Again, we’re going to set the data type as jsonstring, and since this is our last item in our array, we do not need a semicolon after this, so that should be all that we need. I’m going to go ahead and copy this JSON object to use for our other buttons as well, since the only difference, at least on this layout, will be the sort field. We’ll go ahead and set up the rest of our buttons here. We’ll change our sort field to Status and this will of course be “Category”. 

And the last one we’ll do is “DueDate.” Hit OK. So if we save this and go back into browse mode, we’ll click these. Of course they still do not work, because we have to receive the parameters into our script. So we’ll go back into Script Workspace and set the variable of the Context. For this, we’ll use another native JSON function, JSONGetElement. It expects to receive a JSON object of some sort, so for this one, it’s actually going to be captured with the get(ScriptParameter) function, that will actually have a JSON function within it. For this we’re going to specify that key value pair, and for this one we are looking for context. That’s all we need to pull out the value of our context variable form our JSON object. Say OK.

We’ll do the same thing for the sort field, JSONGetElement. Again our JSON is coming through in script parameter, and our key for that key value pair was sort_field. We’ll say ok. Save this script, and come back over here and let’s see if it works.

Looks like this might actually be working! We’re able to sort on task, status, category and due date. We can go ahead and apply the same JSONobject parameter here in our assignees list. We’ll of course have to change the context to assignee, and we will turn our sort field into our first name. I’m going to copy this, that way we can also use the same block of code over here in our last name. I’ll change this to Last. And, I’m just going to verify that, in the script, we are looking for the sort field to be named first and last, so if we go ahead and save this again and we should be able to sort now based off of first name and last name. And that’s it! You’ve now successfully passed in parameters and parsed them using FM’s native JSON Functionality. Thanks for watching.