Useful pattern for getting JavaScript Results into FM Scripts

Here's a cool trick for running JavaScript functions from FileMaker scripts and retrieving the result.

  1. Create a FM script called Exit With Parameters that has just one line:
    Exit Script [ Get ( ScriptParameter ) ]

  2. In the webviewer code, create a function that contains
    FileMaker.PerformScript ( 'Exit With Parameters', dataYouWantToReturn )

  3. Within your FM script do Perform JavaScript in Web Viewer [ "jsFunctionName" ; dataYouWishToProcess ] , and right after that, do Get ( ScriptResult ) to retrieve the value returned by the JS function.

The cool thing is, Perform Javascript in Web Viewer waits for the JS function to complete. The JS function runs a FM script that sets the contents of ScriptResult, which you're now free to use in the rest of your script!

Try it out!
Synchronous JS FM.fmp12 (212 KB)

NOTE: based on my tests, the JS will run synchronously, that is FM will wait for the function to finish, even within Web Direct (unless my test was flawed). However, if you write anything in the JavaScript that causes the function to run asynchronously, FM will not wait for the result.

EDIT 2020-06-07: this requires FileMaker 19.


This is a nice, simple example. Still trying to get my head around the full range of possibilities with JavaScript, particularly with respect to the new functionality in FM19, so this is great to be able to debug and play around with.
Out of interest - where did you learn Javascript? Any specific books / online courses? Or just through working with it?

1 Like

Out of interest - where did you learn Javascript? Any specific books / online courses? Or just through working with it?


Ahhh, interesting, I'm using at the moment - it's really good for a bit of a newbie like me!

1 Like

Here's an example I did for a posting on the regular forum where people were posting long "LET" FMP examples to do what you can do in JS in a single line of code.

This pattern sends the JavaScript to a micro-service where, using the Nashorn library (in Java), the Java micro-service will run JavaScript.

Note: Nashorn has been deprecated in current Java versions in favor of GraalVM.
(A standalone Java Development Kit to execute Java or JVM-based languages (e.g., Scala, Kotlin), dynamic languages (e.g., JavaScript, R, Ruby, R, Python), WebAssembly, LLVM-based languages (e.g., C and C++) -- all FROM Java! )

JS is easy to learn, just get a good IDE like Webstorm and start coding a bit. It all comes when you have a particular need. TONS of examples online. W3Schools is good also.

Running ad-hoc JavaScript is just another method in my existing FMP micro-service (out of about 150 other methods to do whatever).

Happy Coding!

Nice @jwilling !


I’m very much still learning Javascript, it’s a wacky language. I’m loving it it though, especially all the async stuff. I’ve watched some LinkedIn learning and YouTube tutorials, but mostly I just tinker around and look stuff up as I get stuck.

1 Like

I just realized I left a bunch of cruft in the JS in the original example file. If you downloaded it already, please replace the HTML field contents with the following. (I've also replaced the example file above).

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  hello world
			function getEvenElements(arr) {
				const parsed = JSON.parse(arr);
				const filtered = parsed.filter(el => el % 2 === 0);
				FileMaker.PerformScript('Exit With Parameters', JSON.stringify(filtered))
1 Like