JavaScript APIs for actions

Use the following reference information to learn more about the JavaScript APIs that are available for actions when creating your business applications in the low-code designer.

The Script activity for actions is enhanced to support asynchronous requests to servers that are identified as application resources. The following JavaScript functions can be used in the Script activity. For an example of how these functions are used, explore the StartProcess action that is provided in the Workflow Services toolkit.

Restriction: The following functions cannot be used in pre-execution or post-execution scripts.
Table 1. JavaScript functions that can be used in Script activities
Function Description
tw.system.waitForContinueFlow() Call this function to indicate that the script makes an asynchronous call. It prevents the action from continuing to the next step in the flow when the end of the script is reached.
tw.system.continueFlow() When this function is called from an asynchronous callback, it indicates that the asynchronous call is complete and the action can continue to the next step in the flow.
tw.system.performRequest(name, options, callback, errorcallback) Makes an asynchronous call to a server. You must call waitForContinueFlow before calling this function, and the continueFlow function must be called by the callbacks.
Important: If continueFlow is not called, the action cannot continue to completion.
The tw.system.performRequest function has the following parameters:
{string} name
The name of the application resource that identifies the server to call.
{object} options
The details of the request. If the application resource defines the property, then setting it here overrides the application resource value. The following properties are available:
  • {string} path: The relative URL, including all query parameters. When using content-type header "application/x-www-form-urlencoded", add the parameters to the payload instead.
  • {string} method: The HTTP method, such as "GET" (default), "POST", "PUT", "DELETE".
  • {object or string} payload: The payload data to be placed in the request body: an object payload is converted to JSON, unless the content-type header indicates otherwise. To simulate a browser form submit, set the content-type to "multipart/form-data" in the headers, then set the payload to an object in the form. For example,
    {
    <name>: <value>[,
    <name>: <value>]*
    }
    Where <name> and <value> represent form fields. To send a file, <value> must be a DocumentFile business object, which can come from the response of a prior tw.system.performRequest call, or as an input from the calling application. To load the file in the application, see JavaScript APIs that can be used in views.
  • {object} auth: An object that indicates the authentication method to be used:
    • {string} protocol: The authentication protocol, either "OIDC" or "BASIC".
    • {string} username: The username for the BASIC protocol.
    • {string} password: The password for the BASIC protocol.
  • {string} protocol: The request protocol to use, either "https" (default) or "http".
  • {string} hostname: The hostname.
  • {numeric} port: The port number (443 default).
  • {string} ctxRoot: (Optional) A context root ("" default) to add as a prefix to the path.
  • {object} headers: (Optional) HTTP headers; setting the content-type affects the handling of the payload. To simulate a browser form submit, the content-type must be multipart/form-data.
  • {numeric} timeout: (Optional) The request timeout value, in milliseconds.
  • {string} keySuffix: (Optional) Appended to the common group key that is defined in the application resource to dynamically identify a single resource from a set of resources.
  • {string} responseType: (Optional) The expected response type. It must be binary for tw.system.performRequest to handle a binary response and return a DocumentFile business object as the response object to the callback function. The DocumentFile business object has a URL field, which can be used by a view to retrieve the file content.
  • {boolean} parseJSONResponse: (Optional) Specify what the response object will be that is passed to the callback function when the HTTP request returns a JSON response.
    • true - The response object is the JavaScript object that is created by parsing the JSON string.
    • false - The response object is the JSON string.
    • Unspecified (default) - If the value in the content-type header is application/json, then the response is parsed. If the content-header value is more complex, like application/json; charset=UTF-8, then the JSON string is not parsed.
{function} callback
The callback function for handling a successful response. The function takes two parameters: the first represents the response object, and the second contains additional information, such as statusCode, headers.
{function} errorcallback
The error callback function with a single parameter that represents the error object.
tw.system.log(tw.system.model.name, tw.system.model.step.name, logString) Use the tw.system.log API to debug the server-side script by printing logs to the browser console. Logs print to the browser console if you are on a playback server, or if the BROWSER_LOG_LEVEL is set to 3 or higher in your production Application Engine.

Keep the first two parameters as-is and enter your log string in the third parameter.

{string} tw.system.model.name
The name of the action or the nested action in which the current script is defined (or running).
{string} tw.system.model.step.name
The name of the server-side script that is being run.
{string} logString
The specific log string that you want to run.
tw.system.model() The action or nested action in which the current script is defined (or running).
tw.system.jsonReplacer(key, value)

A JSON.stringify replacer that is used for converting JavaScript objects into the JSON format that is required by an action. This must be used for an application data persistence action after retrieving data from the data store. Special handling is performed for lists, dates, and validationErrors.

{string} key
The key that is being converted to a string.
{string} value
The value that is being converted to a string.
tw.system.jsonReviver(key, value)

A JSON.parse reviver that is used for converting the JSON string into JavaScript objects that are required by an action. This can be used for an application data persistence action after retrieving data from the data store. Special handling is performed for lists and dates.

{string} key
The key that is being parsed.
{string} value
The value that is being parsed.