YOUSEFUL / FILEMAKER
Youseful JavaScript: FAQ, examples and tutorial
Learning center · Function reference
Use YsflJS.fmx64 for embedded JavaScript or external Node.js execution. These
examples explicitly select the embedded engine and need no Node installation.
FAQ
Do I need Node.js? Not for mode=inproc. The embedded engine handles standard
JavaScript and JSON. Use mode=process or mode=worker for Node-specific APIs such
as require, and install Node separately for those modes.
What does Run return? The completion value; arrays and objects become JSON. When there is no value, captured console output is used. Output returns console text and Error provides the full error/stack information.
Will variables persist? Embedded and worker globals can persist between calls. Process mode starts a fresh interpreter per call. Reset clears the working scope; Stop ends a worker. Use unique names when sharing an interpreter across scripts.
Can I await a Promise? The current API does not await returned Promises. A Promise result is not a completed HTTP response. Use synchronous calculations for these examples; use the HTTP/Curl plug-ins for their documented network calls.
Examples
Calculate a sum:
YsflJs_Run ( "[1,2,3].reduce((sum, n) => sum + n, 0)" ; "mode=inproc" )Result: 6.
Return an object as JSON:
YsflJs_Run ( "({brand: 'Youseful', count: 3})" ; "mode=inproc" )Result: {"brand":"Youseful","count":3}.
Tutorial
Standalone sample scripts: JavaScript totals.
Goal: calculate line totals from a JSON array without constructing JavaScript source from user data.
- Create
Youseful - JavaScript totals. - Add these steps. The data travels through
fm.param, separate from the code.
Set Variable [ $rows ; Value: "[{\"sku\":\"A1\",\"qty\":3,\"price\":10},{\"sku\":\"B2\",\"qty\":1,\"price\":7}]" ]
Set Variable [ $json ; Value: YsflJs_Run (
"JSON.parse(fm.param).map(row => ({sku: row.sku, total: row.qty * row.price}))" ;
"mode=inproc timeout=15¶param=" & $rows
) ]
Set Variable [ $error ; Value: YsflJs_LastError ]
If [ $error <> 0 ]
Set Variable [ $detail ; Value: YsflJs_Error ]
Show Custom Dialog [ "JavaScript failed" ; $detail ]
Exit Script [ Text Result: $json ]
End If
Show Custom Dialog [ "Line totals" ; JSONFormatElements ( $json ) ]- Run it. Ignoring JSON formatting, expect:
[{"sku":"A1","total":30},{"sku":"B2","total":7}]- Change A1's quantity to 4 and run again. Its total becomes 40.
Troubleshooting
Put param= last on its option line. Use compact JSON without literal line
breaks in that option. Embedded JavaScript has no Node require; select a Node
mode for Node-specific code. If a failure occurs, keep the returned result and
capture Error before another script call replaces the diagnostic.