Script calls
Complex business logic, multi-record transactions, and external services should execute as FileMaker scripts rather than independent Data API CRUD operations. @adk/fmdapi exports two functions for calling scripts:
-
fmFetch: Calls a script and awaits its return value. -
callFMScript: Executes a script asynchronously without waiting for results.
Note Script design: fmFetch calls scripts directly and reads the Exit Script result. Earlier versions wrapped parameters in a callback envelope; ensure scripts return results directly using Exit Script.
Both wrap window.FileMaker.PerformScriptAsync (injected into Web Viewers by FileMaker Pro) and operate inside Web Viewers or WebDirect. See Call FileMaker scripts from an app.
fmFetch: Call a script and get a result
type DashboardResult = {
openInvoices: number;
overdueInvoices: number;
};
const dashboard = await fmFetch<DashboardResult>("Get Dashboard", {
accountId: "123",
});
fmFetch(scriptName, data) passes data directly to Get ( ScriptParameter ) without wrapping layers. If window.FileMaker is initializing, fmFetch retries with exponential backoff.
What the script receives
Read inputs from the top level of Get ( ScriptParameter ):
Set Variable [ $param ; Get ( ScriptParameter ) ]
Set Variable [ $accountId ; JSONGetElement ( $param ; "accountId" ) ] // correct — top level
How the result comes back
fmFetch performs the script through window.FileMaker.PerformScriptAsync and resolves with the Exit Script parameter:
-
A result representing valid JSON is parsed into an object, array, string, number, or boolean.
-
A non-JSON result is returned as a string.
-
An empty result resolves as
undefined.
Warning Explicit Exit Script: Ensure scripts reach Exit Script across all execution paths. A script that halts or displays a modal dialog leaves the JavaScript promise unresolved.
Warning Preserve layout context: Avoid navigating away from the Web Viewer layout in scripts called by fmFetch. Use New Window (off-screen) or Perform Script on Server for context shifts.
Typing the result
Pass a type parameter for TypeScript checking:
const dashboard = await fmFetch<DashboardResult>("Get Dashboard", {
accountId: "123",
});
Warning Runtime validation: Type parameters provide compile-time safety only. Parse responses with Zod schemas to guarantee runtime validity:
const saveResultSchema = z.object({
ok: z.boolean(),
message: z.string().optional(),
});
const result = saveResultSchema.parse(
await fmFetch("Save Contact Preferences", { contactId: "123" }),
);
callFMScript: Fire and forget
Use callFMScript when your app does not need a return value (such as event logging or triggering independent workflows):
callFMScript("Navigate Away From Web Viewer", { layout: "Contacts" });
The script parameter is passed as the second argument — a JSON object or a string — and is stringified automatically. callFMScript returns void; it does not install a callback, so do not call it expecting a result:
// Wrong — result is always undefined
const result = callFMScript("Calculate Total", { orderId: "123" });
// Correct — use fmFetch when you need the result
const result = await fmFetch<{ total: number }>("Calculate Total", {
orderId: "123",
});
FMScriptOption
An optional third argument controls how FileMaker executes the script. Import FMScriptOption:
callFMScript("Navigate Away From Web Viewer", { layout: "Contacts" }, FMScriptOption.RESUME);
FMScriptOption
|
Value |
|---|---|
CONTINUE
|
"0"
|
HALT
|
"1"
|
EXIT
|
"2"
|
RESUME
|
"3"
|
PAUSE
|
"4"
|
SUSPEND_AND_RESUME
|
"5"
|
Use scripts for transactional writes
Data API calls execute independently. Multi-record operations and transactional logic should execute as a single FileMaker script called with fmFetch. See Splitting app logic.
type CreateInvoiceResult = {
invoiceId: string;
status: "created";
};
const result = await fmFetch<CreateInvoiceResult>("Create Invoice With Line Items", {
invoice,
lineItems,
});