Calling Scripts in FileMaker from DayBack

You can use custom actions to fire scripts in your FileMaker Pro solution. Prior to FileMaker 19 you'd have needed to use the fmp:// URL format documented here. And that did not work great in WebDirect. But with FileMaker 19 this all gets much simpler and DayBack provides the following function to call a script in the current FileMaker file in Pro, Go, and WebDirect: 

dbk.performFileMakerScript('ScriptName', event.timeStart);
	

Here's what that looks like as a button action in DayBack, calling a simple FileMaker script named "Show Dialog":

You can learn more about adding buttons to DayBack here: custom button actions.


Script Parameters

In this example, "ScriptName" is the name of the FileMaker script you'd like to run and "event.timeStart" is the script parameter. That particular parameter is the start time of the event and you can find the object names for all DayBack's fields here: field names in DayBack's event object.
Alternatively. you can send the entire event or eventEdit object as your parameter. ("event" is the saved state of the event "editEvent" includes any unsaved changed you've made in DayBack's popover.) That would look like this...
dbk.performFileMakerScript('ScriptName', event)
	

...and contains a ton of information about the event including all its fields, the field names in FileMaker, the events table occurrence in FileMaker, and the FileMaker layout you selected for this calendar source. All this information is in a single JSON object. Here's an example from a simple event: SampleEvent.json

Once the event or editEvent object is in FileMaker, you can parse out individual fields or attributes from that object using FileMaker's built-in JSON functions. For example, here's the event's description followed by the event's ID in FileMaker:

JSONGetElement ( $MyScriptParameter ; "description" ) & " " & JSONGetElement ( $MyScriptParameter ; "eventID" )<br>

Note that some aspects of the object are arrays, not discrete values. For example, DayBack supports multiple values for both status and resource, so those are arrays in the event and editEvent object, so you'd get their attributes in FileMaker by asking for a list instead of an element. Here's how to get the ist of an event's statuses:

JSONListValues ( $MyScriptParameter ; "status" )

Multiple Script Parameters

Sending the "event" or "editEvent" objects as a script parameter is, in a sense, sending multiple parameters since those objects have so many attributes within them. But you may want to send separate multiple parameters by hand. We recommend passing multiple parameters as JSON so that you can grab specific parameter elements using the FileMaker functions JSONGetElement and JSONListValues. 

Here's what that looks like in a button from DayBack, passing two parameters to FileMaker, the event ID and layout name:

dbk.performFileMakerScript('Show Dialog', {ID: event.eventID, layout: event.schedule.layoutName})

In FileMaker, you'd grab the layout name like this:

JSONGetElement ( $MyScriptParameter ; "layout" )<br>

If you want to hard code some parameters in your action, just wrap them in quotes like Reset is here:

dbk.performFileMakerScript('Show Dialog', {ID: event.eventID, operation: "Reset"})<br>

Callbacks & Script Results

The dbk.performFileMakerScript function above actually takes an optional third parameter, which is the callback function you'd like to run in DayBack after the FileMaker script executes. The callback function will receive the script result of your FileMaker script as its parameter. You can see an example of this in the custom actions used to pull statuses and resources from FileMaker: staus and resource examples.
Here's a simpler example. We'll add an exit script result to our "Show Dialog" script and then see that script result show up back in DayBack using a JavaScript alert. Our button now looks like this:
dbk.performFileMakerScript('Show Dialog', {ID: event.eventID, layout: event.schedule.layoutName}, function(result){alert(result.payload)})
Note that the alert function is going to show "result.payload" which is where DayBack puts your FileMaker script result.
Here's our dead-simple FileMaker script:

Here's what it looks like to click that button, perform the FileMaker script, and then get a result back in DayBack:

We hope this helps you build great interactions between FileMaker and DayBack! Please get in touch if you need help.