# Workflow

Workflow helper library.

### Static properties

#### instanceId *string*

The current workflow instance id.

#### instance *WorkflowInstance*

The current workflow instance information.

#### signal *any*

Signal data.

#### signalObject *{ \[key: string]: any; }*

Signal data as an object. If the signal data is not an object, this will be null.

#### tags *{ \[key: string]: any; }*

Metric tags to override the default tags for the generated alerts.

### Methods

#### createSignalUrl

```javascript
createSignalUrl(nextStep: string, token: any, title?: string, redirectUrl?: string)
```

Create a signal url.

**Parameters**

&#x20;   **nextStep&#x20;*****string***

&#x20;       The ID or name of the next step.

&#x20;   **token&#x20;*****any***

&#x20;       The token or signal data. This corresponds to a branch name of the given next step

&#x20;   **title&#x20;*****string***

&#x20;       (optional) The title of the signal.

&#x20;   **redirectUrl&#x20;*****string***

&#x20;       (optional) The redirect URL to send the user to after clicking the signal URL. URL parameters 'instanceId', 'stepId' and 'signal' will be added to the URL.

**Return type**

&#x20;   string

**Examples**

```javascript
// Create a signal URL using the given next step: Workflow.createSignalUrl
Workflow.createSignalUrl('nextStepId', 'token')

// Create a signal URL and redirect to a workspace page
// (This assumes you have a workspace page in your Rulecube org with the URL 'your-landing-page')
Workflow.createSignalUrl('nextStepId', 'token', 'Token title', '/#/w/your-org/page/your-landing-page')
```

#### createNextSignalUrl

```javascript
createNextSignalUrl(token: any, title?: string, redirectUrl?: string)
```

Create a signal url using the configured next step.

**Parameters**

&#x20;   **token&#x20;*****any***

&#x20;       The token or signal data. This corresponds to a branch name of the next step.

&#x20;   **title&#x20;*****string***

&#x20;       (optional) The title of the signal.

&#x20;   **redirectUrl&#x20;*****string***

&#x20;       (optional) The redirect URL to send the user to after clicking the signal URL. URL parameters 'instanceId', 'stepId' and 'signal' will be added to the URL.

**Return type**

&#x20;   string

**Examples**

```javascript
// Create a signal URL using the configured next step: Workflow.createNextSignalUrl
Workflow.createNextSignalUrl('token')

// Create a signal URL and redirect to a workspace page
// (This assumes you have a workspace page in your Rulecube org with the URL 'your-landing-page')
Workflow.createNextSignalUrl('token', 'Token title', '/#/w/your-org/page/your-landing-page')
```

#### sendHubSignal

```javascript
sendHubSignal(name: string, data: { [key: string]: any; })
```

Send a signal to all Hub clients connected to the workflow instance.

**Parameters**

&#x20;   **name&#x20;*****string***

&#x20;       The name of the signal.

&#x20;   **data&#x20;*****{ \[key: string]: any; }***

&#x20;       The data to send.

**Return type**

&#x20;   void

**Examples**

```javascript
```

#### createWebhookUrl

```javascript
createWebhookUrl(stepId: string)
```

Create a webhook url for a specific workflow step.

**Parameters**

&#x20;   **stepId&#x20;*****string***

&#x20;       The ID of the workflow step.

**Return type**

&#x20;   string

#### createNextWebhookUrl

```javascript
createNextWebhookUrl()
```

Create a webhook url for the next workflow step.

**Parameters**

**Return type**

&#x20;   string

**Examples**

```javascript
// Assuming the next step is an Upload step of type 'Webhook'
let webhookUrl = Workflow.createNextWebhookUrl('stepId')

// Use imaginary payment provider to create a payment and set the webhook URL:
// This payment provider will call the webhook URL when the payment is completed
Http.post('https://payment-provider.com/create-payment', { amount: 100, currency: 'EUR', webhookUrl: webhookUrl })
```

#### queue

```javascript
queue(name: string, version: string, input: { [key: string]: any; }, options?: { [key: string]: any; }, tags?: string[])
```

Queue another workflow ruleset to start.

**Parameters**

&#x20;   **name&#x20;*****string***

&#x20;       The name of the workflow ruleset.

&#x20;   **version&#x20;*****string***

&#x20;       The version of the workflow ruleset.

&#x20;   **input&#x20;*****{ \[key: string]: any; }***

&#x20;       The input data for the workflow.

&#x20;   **options&#x20;*****{ \[key: string]: any; }***

&#x20;       (optional) The options for the workflow.

&#x20;   **tags&#x20;*****string\[]***

&#x20;       (optional) The tags for the workflow.

**Return type**

&#x20;   void

#### queueLibrary

```javascript
queueLibrary(library: Function, input: { [key: string]: any; }, options?: { [key: string]: any; }, tags?: string[])
```

Queue another workflow library ruleset to start.

**Parameters**

&#x20;   **library&#x20;*****Function***

&#x20;       The library that is a workflow ruleset.

&#x20;   **input&#x20;*****{ \[key: string]: any; }***

&#x20;       The input data for the workflow.

&#x20;   **options&#x20;*****{ \[key: string]: any; }***

&#x20;       (optional) The options for the workflow.

&#x20;   **tags&#x20;*****string\[]***

&#x20;       (optional) The tags for the workflow.

**Return type**

&#x20;   void

#### getDocuments

```javascript
getDocuments()
```

Get all documents from the workflow. Requires for the calling workflow step to have 'Access Documents' enabled.

**Parameters**

**Return type**

&#x20;   WorkflowDocument\[]

#### getDocument

```javascript
getDocument(documentTypeName: string)
```

Get a document from the workflow. Requires for the calling workflow step to have 'Access Documents' enabled.

**Parameters**

&#x20;   **documentTypeName&#x20;*****string***

&#x20;       The name of the document type. Should match one defined under the 'Document Types' section of the workflow Settings.

**Return type**

&#x20;   WorkflowDocument

#### getDocumentDataUri

```javascript
getDocumentDataUri(documentTypeName: string)
```

Get the contents of a document as a data URI. Requires for the calling workflow step to have 'Access Documents' enabled.

**Parameters**

&#x20;   **documentTypeName&#x20;*****string***

&#x20;       The name of the document type. Should match one defined under the 'Document Types' section of the workflow Settings.

**Return type**

&#x20;   string

#### saveDocument

```javascript
saveDocument(documentTypeName: string, dataUri: string, fileName?: string)
```

Save a document to the workflow.

**Parameters**

&#x20;   **documentTypeName&#x20;*****string***

&#x20;       The name of the document type. Should match one defined under the 'Document Types' section of the workflow Settings.

&#x20;   **dataUri&#x20;*****string***

&#x20;       The Base64 data URI of the document.

&#x20;   **fileName&#x20;*****string***

&#x20;       (optional) The name of the file.

**Return type**

&#x20;   void

**Examples**

```javascript
// This assumes there is a Document Type defined in the workflow with the name 'MyDocument'.'
Workflow.saveDocument('MyDocument', 'data:text/plain;base64,SG93ZHkgcGFydG5lciE=');

// The 3rd parameter is optional and can be used to set the file name.
Workflow.saveDocument('MyDocument', 'data:text/plain;base64,SG93ZHkgcGFydG5lciE=', 'MyFile.txt');
```

#### getDocumentTypes

```javascript
getDocumentTypes()
```

Get the document types defined in the workflow settings.

**Parameters**

**Return type**

&#x20;   WorkflowDocumentType\[]

**Examples**

```javascript
Workflow.getDocumentTypes() // Get the document types defined in the workflow settings
```

#### removeDocuments

```javascript
removeDocuments(documentTypeName: string)
```

Remove all documents of a specific type from the workflow instance.

**Parameters**

&#x20;   **documentTypeName&#x20;*****string***

&#x20;       The name of the document type. Should match one defined under the 'Document Types' section of the workflow Settings.

**Return type**

&#x20;   void

**Examples**

```javascript
// This assumes there is a Document Type defined in the workflow with the name 'MyDocument'.'
Workflow.removeDocuments('MyDocument');
```

#### setProgress

```javascript
setProgress(completed: number, total: number)
```

Set the progress of the workflow.

**Parameters**

&#x20;   **completed&#x20;*****number***

&#x20;       The number of completed items.

&#x20;   **total&#x20;*****number***

&#x20;       The total number of items.

**Return type**

&#x20;   void

#### setStage

```javascript
setStage(stage: string)
```

Set the custom stage of the workflow.

**Parameters**

&#x20;   **stage&#x20;*****string***

&#x20;       The custom stage of the workflow.

**Return type**

&#x20;   void

**Examples**

```javascript
Workflow.setStage('My custom stage');
```

#### setName

```javascript
setName(name: string)
```

Set the name of the workflow instance.

**Parameters**

&#x20;   **name&#x20;*****string***

&#x20;       The name of the workflow instance.

**Return type**

&#x20;   void

#### stop

```javascript
stop(message?: string)
```

Stop the workflow gracefully. State changes will be saved.

**Parameters**

&#x20;   **message&#x20;*****string***

&#x20;       (optional) The message to log.

**Return type**

&#x20;   void

#### getPreviousAlerts

```javascript
getPreviousAlerts()
```

Get the previous alerts for the current step. These are the alerts that were by the direct previous step(s).

**Parameters**

**Return type**

&#x20;   Alert\[]

#### clearAlerts

```javascript
clearAlerts()
```

Clear all alerts from the workflow instance.

**Parameters**

**Return type**

&#x20;   void

#### getPreviousTasks

```javascript
getPreviousTasks()
```

Get the previous tasks for the current step. These are the tasks that link to the current step's input node.

**Parameters**

**Return type**

&#x20;   WorkflowTask\[]

#### getPreviousTask

```javascript
getPreviousTask()
```

Get the most recent previous task for the current step.

**Parameters**

**Return type**

&#x20;   WorkflowTask

#### getPreviousTaskComment

```javascript
getPreviousTaskComment()
```

Get the comment of the most recent previous task for the current step.

**Parameters**

**Return type**

&#x20;   string

#### assignUser

```javascript
assignUser(userId: string)
```

Assign a user to the workflow instance, so they can access it.

**Parameters**

&#x20;   **userId&#x20;*****string***

&#x20;       The ID of the user to assign.

**Return type**

&#x20;   void

**Examples**

```javascript
Workflow.assignUser('userId');
```

#### unassignUser

```javascript
unassignUser(userId: string)
```

Unassign a user from the workflow instance, so they can no longer access it.

**Parameters**

&#x20;   **userId&#x20;*****string***

&#x20;       The ID of the user to unassign.

**Return type**

&#x20;   void

**Examples**

```javascript
Workflow.unassignUser('userId');
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rulecube.com/language-reference/workflow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
