FileMaker Pro 9: Script Debugging - 9 minutes, 36 seconds
TRANSCRIPT
Hi, I'm Geoff Coffey the co-author of FileMaker Pro 9: The Missing Manual and in this screen cast we're going to talk about script debugging. In particular we're going to script debugger window, the data viewer window and how to use these two windows to help you find bugs in your own scripts.
I have a script in this database that has a bug in it. I'll show you right now. I'm sitting on this job record and I run this script called "create invoice for job" and FileMaker tells me this job has no unbilled activity.
You'll have to take my word for it but I looked at all the details under the hood and I do in fact have unbilled activity on this job. So for some reason my script isn't working. Now if you're a FileMaker scripter who's never used the script debugger you might look at a problem like this and think, "Ugh. I have to go figure out what's going on. I'm gonna have to do a lot of hunting and digging."
But with the script debugger it's really, really easy to find problems like this and that's because instead of your script running from start to finish really quickly with no visibility the script debugger lets you see exactly what's happening while your script runs.
I'll show you. I'm gonna go to the Tools menu and choose Script Debugger and here it is down here. Real quick I'll zoom in on that so you can see what it looks like. It's not too exciting. It's a simple floating window, has some buttons across the top that don't have labels and we'll look at those in a minute. Big white area labeled here labeled "script - none." So I see I'm not running any script now but if I were it would show me the name of my script and it would in fact show all the steps in my script right here.
In this active scripts area here shows me all my scripts that are running because, of course, one script could be in the middle of running and call another script, which could then run and perhaps call another. So they'll be listed down here. There's some other controls in this script debugger as well.
Now the script debugger works hand in hand with something else called the Data Viewer. If I go to my Tools menu and chose Data Viewer it pops up here. The data viewer is perhaps even cooler than the script debugger. It shows me data from my script. So it has two tabs really, we're gonna focus on the current tab right now.
The current tab shows the name, value and type of every field and every variable that your script deals with. So we'll see once we start debugging scripts that we can see which fields it's using and what the value is in that field. And even better as my script runs as I go through it bit by bit slowly at human speed so that I can watch it happen, the data viewer will show me when those variables change or when those field values change.
So let's dive into it and see what happens. I'll zoom back out and I'll just go to the scripts menu and run my script. Now because I have the script debugger open down here my script didn't run like it normally does. In fact, nothing has happened at all yet as far as my script is concerned. Instead the script just opens up inside the script debugger.
If we look down here we can see these buttons have come to life. Now it tells me the name of the script it's running and I see all the steps that are in that script down here. I see the name of the script down here, what file it comes from. So I'm seeing information about my script. Now I know my script hasn't run yet because my step indicator which is right here, this blue arrow, is pointing right to the top of the script.
What that means is that's the next script step that FileMaker is going to run. So if I start running this script that's where it goes next, it runs that step and then the next and then the next like you would expect from a normal script.
But because I'm in the debugger, if I run that step, just that step will run and then I'll be sitting right here and you'll see my little blue arrow will march forward down this gray bar as I go, as I run step by step by step. At any point I can then look at the data viewer, look at my database itself, look at the script in the debugger and make some decisions about what's happening.
Now it turns out there are actually several different ways to step through a script. These three buttons here control your script stepping. The first one says, "Okay, I'm looking at this script right now and I want to step down to the next line." So if I were to click this, I won't do that now, but if I were to this blue arrow would move down to here, to this next line right here and it would have run this step.
Now this is a perform script step so what it would have really done is run an entire script. That script would run from start to finish just like normal, quickly, without any control and then I would be sitting right here my blue arrow pointing at the second step and FileMaker would say, "What do you want to do next?"
In this case I want to actually see the details of what's happening inside this subscript so I'll use the second button which is labeled "step into." It's not actually labeled but it has this popup yellow tool tip that will tell me what it is.
The "step into" button says, "Don't step to the next line in this script. But instead go into the subscript and look at its first line and let me step through it line by line so that I can look at the full detail of what subscript." Now if you're not on a "perform script" script step then step and step into do exactly the same thing, they run the current step and move to the next line.
I'm gonna go ahead and push the "step into" button now and we'll see what happened. I push it and instantly it tells me my script is now "find unbilled activity" so I'm in a different script than I was before. All my steps have changed because I'm looking at the steps in that subscript. And down here under "active scripts" I can see "create invoice for job." That's the script I was in before; it's at the bottom of the list because it's the first script that was running.
And it in turn called, "find unbilled activity." It's in bold because it's the script that is in fact running right now. I can also see what parameter was passed to this script. It was passed the parameter of two. So if you're looking at script parameters you have full information about those down here as well.
And again, my blue arrow is up here at the top of the script. FileMaker doesn't run comments so they ignored. So I'm sitting at the top of the script ready to run that first step. Now I can use my step button to perform that script.
I'm gonna zoom back out first though so we can see what happens. I'm sitting here on my jobs layout and this next script step is "go to layout expenses." If I click the step button you see that FileMaker went to the expenses layout. So not only did I get to run just that one step but I can see it happening behind the scenes in my database. It then says, "enter find mode?" Boom, I'm in find mode. It says, "set field?" Boom, I set a field. It says, "set error capture on?" There's nothing to see with that step but it happened and performed the find and turned error capture off and we see that all those things occurred.
Now this job doesn't actually have any expenses so it found zero records here, which is expected. This next script step is a set variable script step so since we're talking about data now let's turn our attention to the data viewer.
If we look at the data viewer now it has two entries, the Job ID from the Time Slips table and the job ID from the Expenses table. In either case the value is blank and it tells me those are number fields. Those are listed here because if we come down here to our script you'll see that it uses that Job ID field. It uses it from both tables if we look at the whole script.
So this data viewer in its current tab right here shows me every field that I'm using right away so that I wanted to look at what's in them I can. Now there's no variables listed here because I haven't actually created any variables in my script yet. That's what the next step is about to do.
So I'll push the step button again. Now my set variable step is run and my data viewer has acquired one new entry in the list. If I zoom in I see that it's the unbilled items variable with a value of zero. So that looks good. Unbilled items is zero because I have no expenses. I'm gonna go ahead and step again. Now I'm gonna go to the time slips layout, go to Find mode, set a field, turn error capture on, run the script. Notice my blue arrow is moving down the script as I go.
Turn error capture off and now I see I have one record found. That's the one time slip record for this job. That's what I thought I had. For some reason the script tells me it has no unbilled activity but I see it just found some unbilled activity. So I still haven't found my bug. Things are still working okay.
I'm gonna run the next set variable script step and if we want to look closely at that it sets the unbilled items variable to a different calculation. Now I can't see it very well in the debugger but what I can do is I can click the Edit Script button. It's the second to last button on the top of the script debugger. If I click it my Script Edit window pops right up and from here I can double click and see full details.
I'm setting a variable called Unbilled Items and here's my calculation. Let's go ahead and zoom in on that. It says unbilled items plus the current found count. So I'll take the number of unbilled items I found before and I'll add my time slip found count to it and that'll be the total number of unbilled items for this job.
All of this looks good. It makes sense. I just wanted to be able to see that. And you can see in the debugger that you can dive in and get all the details you want. I'll go ahead and cancel this and this and close my script window.
I'm still looking at my script debugger. Even though I went in to all that detail I haven't stopped debugging so my FileMaker is still sitting here on this next set variable line ready to roll. I'll go ahead and step over it and check my data viewer to see what happened. And I instantly see that something went wrong. Let me zoom in here so you can see this.
Now I have unbilled items from before with a zero and I have unbilled items again with a one. Of course the data viewer knows when two variables are the same variable so it would only show that one time. It didn't in this case. It showed it twice and so I know I have a problem and if I look really closely I see that this unbilled items is spelled with two L's which is correct. This unbilled items is spelled with one L which is not correct.
That's a really common mistake to make in FileMaker. You misspell a variable name and unfortunately when your script runs it doesn't warn you or tell you, you spelled it wrong it just assumes you really wanted two different variables.
So I see right away I have my problem. The data viewer and the script debugger made it completely obvious and I don't have to spend hours trying to track that down.