Globals
The DataSapien Mobile SDK embeds a JavaScript engine and runs scripts you author on the Orchestrator. This page documents the globals available inside those scripts — they exist only in the embedded engine and have no equivalent in the native iOS / Android / Flutter / React Native SDKs.
Scripts can be authored in four places: Journey Script steps, Journey conditional sequence flows, Rule script actions, and Script-sourced MeData Definitions. The available globals vary by context — see the table below.
Script contexts
| Context | Available globals |
|---|---|
| Journey — Script step | onSuccess, onError, all service singletons, fetch, JourneyContext, showView, showProgressView. |
| Journey — Conditional flow | JourneyContext only. Script must be synchronous and use return true | false. Do not call onSuccess or onError. |
| Rule — Script action | onSuccess, onError, service singletons, fetch. No JourneyContext, no UI helpers. |
| MeData Definition — Script source | onSuccess, onError, service singletons, fetch. No JourneyContext, no UI helpers. |
Functions
onSuccess
onSuccess(success): void
Terminates the current Script step / script action on the success path. The SDK uses this call to know the script is finished and to follow the success transition. Every Script step must terminate by calling either onSuccess or onError exactly once; the script's return value is ignored. Not available in conditional flow scripts.
To signal failure, call onError instead — do not call onSuccess(false).
Parameters:
success: boolean — passtrueto advance.
onError
onError(error): void
Terminates the current Script step / script action on the failure path. The SDK records the error and follows the failure transition. Use this from a catch block, or any time the script cannot complete its work. Not available in conditional flow scripts.
Parameters:
error: Error | object | string — anErrorinstance (preferred), or any object with amessage(string) and optionalline(number) property. The SDK reads.messageand.linefrom the value and logs them.
JourneyContext.getValue
JourneyContext.getValue(name): any
Reads a value an earlier step wrote into the JourneyContext. Returns undefined if absent. Available only in Journey Script steps and conditional flow scripts.
Parameters:
name: string — variable name.
JourneyContext.putValue
JourneyContext.putValue(name, value): void
Writes a value into the JourneyContext for later steps to read. Overwrites any existing value with the same name. Available only in Journey Script steps.
Parameters:
name: string — variable name.value: any — value to store.
showView
showView(htmlString): void
Renders an HTML string full-screen in the Journey's WebView. The script pauses until the HTML posts a submit message back to the SDK (see "Submit from custom HTML" below). Available only in Journey Script steps.
Parameters:
htmlString: string — a complete HTML document or fragment to render.
showProgressView
showProgressView(show, message): void
Shows or hides a non-dismissable progress overlay while long-running work runs. The same function both shows and hides the overlay — there is no separate hideProgressView. Available only in Journey Script steps.
Parameters:
show: boolean —trueto show the overlay,falseto hide it.message: string — text shown under the spinner when showing. Pass an empty string""when hiding.
Pair every showProgressView(true, …) with a showProgressView(false, "") — typically in a finally block so the overlay is hidden on both success and failure.
fetch
fetch(url, options?): Promise<any>
Performs an HTTP request and returns a Promise. Available in Journey Script steps, Rule script actions, and Script-sourced MeData Definitions (not in conditional flow scripts, which must be synchronous).
The Promise resolves with the parsed JSON body of the response — not with a browser-style Response object. There is no .status, .headers, .ok, or .json() to call. If the response body is not valid JSON, the Promise rejects.
Parameters:
url: string — the request URL. Rejects with"Invalid URL"if it cannot be parsed.options: object (optional) — request options:method: string — HTTP method (e.g."GET","POST"). Defaults toGET.headers: object (string → string) — request headers. No defaults are added — setContent-Typeyourself when sending a body.body: string — request body. Must be a string; objects are not auto-serialized. Encoded as UTF-8.
The Promise rejects with an error message string on: invalid URL, network error, empty response, or a response body that is not valid JSON.
Conditional flow scripts
Scripts attached to a conditional sequence flow (the line between two steps in the Journey Flow Designer) are different from Script steps:
- They run synchronously and use a plain
returnstatement —async/awaitis not supported. - Do not call
onSuccess— it has no meaning in this context. - If a step has multiple outgoing conditional flows, the SDK follows the first one whose script returns
true. Order is not guaranteed — keep your conditions mutually exclusive.
return JourneyContext.getValue("banking_consent") === "true";
Patterns
Async work in a Script step
try {
const data = await someAsyncCall();
JourneyContext.putValue("data", data);
onSuccess(true);
} catch (err) {
onError(err);
}
Calling an HTTP API with fetch
try {
const result = await fetch("https://api.example.com/users/42", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Ada" }),
});
JourneyContext.putValue("user", result);
onSuccess(true);
} catch (err) {
onError(err);
}
result is already the parsed JSON object — do not call .json() on it. JSON.stringify the body yourself; the SDK does not serialize objects automatically.
Progress overlay around long-running work
showProgressView(true, "Crunching numbers…");
try {
const result = await someLongRunningWork();
JourneyContext.putValue("result", result);
onSuccess(true);
} catch (err) {
onError(err);
} finally {
showProgressView(false, "");
}
Submit from custom HTML (showView)
When you render your own HTML with showView, the HTML must signal submit back to the SDK to let the Journey advance. The native Screen Designer's Button component does this automatically; in custom HTML you wire it yourself:
const html = `
<h1>Hello!</h1>
<p>Tap continue when you're ready.</p>
<button id="continueBtn">Continue</button>
<script>
document.getElementById('continueBtn').addEventListener('click', () => {
window.webkit.messageHandlers.formHandler.postMessage({ type: 'submit' });
});
</script>
`;
showView(html);
The submit message itself advances the Journey to the next step — do not also call onSuccess after showView, or the Journey will advance twice. onSuccess and submit are alternative ways to release a Script step; use exactly one.
The window.webkit.messageHandlers.formHandler.postMessage({ type: 'submit' }) call works on both iOS and Android — the SDK injects the same bridge on both platforms.