Caseblocks JavaScript API Reference
The Csaeblocks scripting environment allows you to wriite javascript code that will allow you to receive, modify and return case data to your environment. Scripts can be executed by lifecycle event hooks, Case Tasks or Webhooks.
You can modify the case data, perform external calls, perform calculations etc to modify your case data, and then call either the exit or fail function to end the case processing.
exit(payload)
Exit returns the object to Caseblocks with the intention of updating the case data with this object
fail(message)
If you wish to halt the case operation, calling fail will do this and pass the message back to the user as a failure message
log(data)
Logs to the system and also logs against the calling function eg Task
Available Variables
payload
This contains the case data as an object, updating this data and returning it in an exit call will pass it back to Caseblocks and the case data being saved will be updated with it.
The c
Endpoint API
To execute a task, send a POST request to:
POST /api/1/endpoint/execute
Request Body
Send a JSON or form-encoded body with the following fields:
name
: The name of the function/task to execute.sourceCode
: The JavaScript code to run.inputDocument
: A JSON string representing the input payload.options
: A JSON string with options/context for the task.metadata[case_type_code]
: (optional) Used for function identification.metadata[account_nickname]
: (optional) Used for function identification.
Example
{
"name": "test_function",
"sourceCode": "payload.result = 'ok'; exit(payload);",
"inputDocument": "{\"foo\": \"bar\"}",
"options": "{\"current_user\": {\"id\": 1, \"email\": \"user@example.com\"}}",
"metadata[case_type_code]": "demo",
"metadata[account_nickname]": "acme"
}
The options
Object
The options
object provides context and parameters to your task code. It can include:
current_user
: Information about the user running the task.params
: Custom parameters for the task.changed_fields
: Object showing before/after values for changed fields.requestContext
: Transaction context (set automatically).authorization
: Authorization token (set automatically if present).
Example
{
"current_user": {
"id": 21,
"email": "acme@caseblocks.local",
"display_name": "Acme",
"account_code": "acme"
},
"params": {
"x": "1"
},
"changed_fields": {
"current_state": ["Verified", "Not Started"],
"_title": [null, "test"]
}
}
Writing Task Code
Your sourceCode
should use the provided payload
and call exit(payload)
to return a result. You can also use fail(message)
to indicate failure.
Example Task
payload.status = 'processed';
exit(payload);
Handling Errors
if (!payload.foo) {
fail('Missing foo!');
}
exit(payload);
Example cURL Request
curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d '{"metadata[case_type_code]": "bulk_uplift","name": "test","sourceCode": "payload.test=\"success\"\n exit(payload)","inputDocument": "{\"title\": \"sample\"}","options": "{}","metadata[account_nickname]": "ccexpo"}' \
"http://localhost:8000/api/1/endpoint/execute"
For more details, see the README.md and the callEndpoint
implementation.
Caseblocks
Type: Reference to the Caseblocks node object.
This is a reference to the Caseblocks node npm package
You can use this to interact with objects in your Caseblocks environment.
requestContext()
const context = requestContext();
Retrieves transactional data that can be passed to any further scripting calls
{
"txnID":"2f7ab749-260e-41f8-899d-ccee93ec84bd","txnSource":"V2API/policy_holder/preSave,CBFunc|/cb/policy_holder/preSave|2025-05-21T10:02:22.561Z"}
generateGuid()
const myGuid = generateGuid();
Generates a new GUID (Universally Unique Identifier) as a string.
94ad94ba-105a-4516-b9d9-07da898fed37
graphql(query, variables, url)
graphql(`
query GetCase($id: ID!) {
case(id: $id) {
title
status
}
}
`, { id: "1234" })
.then(response => {
// handle GraphQL response data
})
.catch(error => {
// handle error
});
Executes a GraphQL query or mutation against the default or specified GraphQL endpoint.
- query: A string containing your GraphQL operation.
- variables: An object mapping variable names to values for the query.
- url (optional): Specify a custom GraphQL endpoint; defaults to the system’s standard endpoint.
- Returns: A Promise resolving with the JSON response.
Use Case: Reading or modifying Caseblocks data (e.g., creating, updating, or fetching cases) in a structured, robust manner.
Collecting workspace informationHere is Markdown documentation for interacting with Expressive5, focusing on the options
object and how to define and call tasks.