Cancel a FileMaker Script – with Easy Clean-up

demo file:  Cancel_Clean_Up.fmp12

As FileMaker developers, sometimes we write scripts that take a long time to process data. Maybe it’s because we’re looping over many records gathering up a report. Or maybe we have set up a process — say for generating a new project, invoice or service order — that involves gathering up a list of tasks, and it takes time to do this.

Sometimes a user wants to stop things, right in the middle of the process! Little do they know that our script has only looped over 10 percent of the records, or set flags in half of them, etc.

What to do now?

Cancel a FileMaker Script – with Easy Clean-up

Well, what if there were a simple way to allow the user to cancel the script even in the middle of performing its task, then have another script take over and clean up as if nothing ever happened? If that is what you need then you are in the right place.

Of course, first you need to decide what kind of behavior you want your script to allow. There are many times when you need a script to complete all the way to the end, as if it were a transaction. In those cases you’ll want to have Allow User Abort [Off] and not allow the user to cancel the script. This is a different use case, and beyond the scope of this example.

But, in cases where it would be OK to cancel the script, as long as you ensure some way that clean-up happens, then this example will be perfect for you.

This is a simple example but you can very easily expand on it for any solution you have. Check out the demo file to see the script, and example of how to use it.

OnTimer Standing By

The crux of this solution is that it relies on an OnTimer Script Trigger.

kick_off_script

To start things off, we initialize a global variable (a semaphore) that says we are not done (“Uncompleted”). Then right away we install an OnTimer Script Trigger. What OnTimer does is simply wait. And wait. And wait. If this script is cancelled then the clean-up script (see below) will be triggered. But the cool thing is that OnTimer is happy to wait until either the script cancels or the script ends — and in either case it will get executed.

Clean-up, Clean-up

The most important part about the clean-up script is that it needs to kill the OnTimer script right away … otherwise you’ll end in an endless loop.

Ontimer script also found in demo file

The rest of the script is pretty straightforward. In the example we are just showing messages based on the global variable. If the “$$_status” variable has “Uncompleted” then it means the main processing script was cancelled. If it instead has “Completed” then the processing script managed to get to the end and complete the task at hand.

So in the context of a real solution you can make sure the user is taken back to a familiar layout and maybe even delete some records if you need to. You have lots of control.

Check out the demo file Cancel_Clean_Up.fmp12. It’s unlocked, so you can get to the script examples shown above.

Hope you find this useful. Enjoy!

– Vince

Leave a Reply