Calling Scripts in DayBack from FileMaker

Introduction

The new JavaScipt options introduced in FileMaker 19 mean that your FileMaker scripts can call JavaScript functions in the web viewer by name. So if you know the name of a function you wish to run, you can call it using FileMaker's " Perform JavaScript in Web Viewer" script step.

That's great if you know the name of a function you wish to run. But more often, the function you want to run is a function of your own: something you want to add to DayBack. So what you often want to do is

  1. Write a new JavaScript function,
  2. Register that function with DayBack,
  3. Call that function from within one of your FileMaker scripts, and
  4. Retrieve the results of that function back in FileMaker.

The following example will show you how to do that. And it's easier than it sounds.

Example: Batch Editing Selected Events

Here's the use case: you'd like to select several events in DayBack calendar and then run a FileMaker operation against each of the selected events. Maybe you want to change all their statuses at once. So the JavaScript function you need to write is a simple one to gather the IDs of those events which have been selected in DayBack. Once you get that list of IDs back in FileMaker, your FileMaker script can change their statuses or perform any operation on them that you can dream up.

Here's how to make that happen.

1. Write a new JavaScript Function

Our function will assemble a list of the selected events and pass that back into a new script in our FileMaker file. We need to pass the result into a new script in FileMaker since the "Perform JavaScript in Web Viewer" script step doesn't accept a callback script as a parameter.

Here's the function:

//Return Selected

//Purpose:
//Creates and registers a function that can be called from FileMaker to Return
//Multi-Selected Events from DayBack to the calling script

//Action Type: Custom App Action - Before Calendar Render
//Prevent Default Action: No

//More info on custom app actions here:
//https://docs.dayback.com/article/140-custom-app-actions


//function for writing selected events back to FM as JSON
function getSelectedEvents(){
  //get multi select object
  var multiSelect = seedcodeCalendar.get('multiSelect');
  //pull events from object and add to new event
  var events = {};
  for ( var event in multiSelect ) {
    var eventId = multiSelect[event].event.eventID;
    events[eventId] = multiSelect[event].event;
  }
  //create JSON and send to FileMaker
  var result = JSON.stringify(events);
  dbk.performFileMakerScript('Sample Receive Payload - DayBack',result);
}

//register the function
dbk.registerFunctionCall('getSelectedEvents',getSelectedEvents);

Don't do anything with this yet, but note that the name of our callback script is "Sample Receive Payload - DayBack". You'll want to create a very simple FileMaker script in your file with the same name. The script is just a single line:

Set Variable [ $resultFromDayBack ; Value ; Get ( ScriptParameter ) ]

That's a generic FileMaker script you'll reuse anytime you need to get results from a JS function back into FileMaker.

2. Register That Function with DayBack

To add this function to DayBack, create a new App Action. Give it any name you'd like and select the trigger as "Before Calendar Render." Set "Perform Default Action" to "No" and paste in the entire JavaScript function above, including the comments. The last line in the function registers that function with DayBack as something we can now call by name.

3. Call That Function From Within One of Your FileMaker Scripts

Now you'll write the FileMaker script that will call your JavaScript function and get the results back in the global variable $$resultFromDayBack. Here's a very simple version of that script:

The first line is all you need to customize at first: that's where you specify the name of the JavaScript function you'd like to run.

Line 10 prepares the instructions you'll send to DayBack and these will likely be the same for each script you'd like to run, so this whole script can be reused except for lines 8 and 19. Here's the Value for that script since you can't see the whole thing on our screenshot:

JSONSetElement ( "{}" ; 

	["payload" ; "" ; JSONString];
	["status" ; 200 ; JSONNumber];
	["error" ; $error ; JSONNumber];
	["dbk" ; 1 ; JSONBoolean];
	["callback" ; $function ; JSONString];
	["keepReference" ; 1 ; JSONBoolean]

)

Line 12 calls a script that's already in DayBack: one we designed to receive these kinds of instructions for calling subscripts. You don't need to change that line at all.

4. Retrieve the Results of That Function Back in FileMaker

That's already happening in lines 14 and 15 which clean up the global variables created by the callback script "Sample Receive Payload - DayBack." Then line 19 just pulls out the IDs for each of the selected events.

You're done and can now pass $selectedIDs on to whichever script wants to loop through those IDs, find each record, and change its status.

Some final thoughts. Note that "JSONListKeys" in Line 19 retrieves the IDs from what is a much larger payload of information about each selected event. (If you want to see the whole payload, check out the value in $result.) The IDs are all you need if all your selected events are  from the same table in FileMaker (the same "calendar" in DayBack). But if you may be selecting events from different tables, the additional information in the $result payload can tell you which calendar each event came from. 

For example, this FileMaker calc will return the calendar name for an event with an ID of 12345:

JSONGetElement ( JSONGetElement ( JSONGetElement ( $resultfromDayBack ; "TD011222" ) ; "schedule" ) ; "name" )

We asked for "name" in that calc, but could have asked for "layoutName" to get the name of the layout you likely want to use when searching for this event (as that layout name will be different for different tables).

Another example - pass parameters to the registered function in DayBack

Everything you set in the $payload variable above can be accessed as an argument from your DayBack function. Let's say we want to initiate a custom modal in DayBack from a FileMaker script. Our action in DayBack would look like this:

//Function for showing a modal in the calendar
function showCustomModal(data){
  //get message contents
  var title = data.title;
  var message = data.message;
  var buttonLabel = data.buttonLabel;

  //show the message using DayBack's built-in modal
  utilities.showModal(title, message, buttonLabel);
}

//register the function
dbk.registerFunctionCall('showCustomModal',showCustomModal);

And, In this scenario, we can follow the script in step 3, changing the $payload variable to look something like this:

JSONSetElement ( "{}" ; 

	["payload" ; "" ; JSONString];
	["title" ; "Message from FileMaker" ; JSONString];
	["message" ; "You've just received a message from FileMaker" ; JSONString];
	["buttonLabel" ; "" ; JSONString];
	["status" ; 200 ; JSONNumber];
	["error" ; $error ; JSONNumber];
	["dbk" ; 1 ; JSONBoolean];
	["callback" ; $function ; JSONString];
	["keepReference" ; 1 ; JSONBoolean]

)