Scripting with workflow tokens

In this recipe, we have taken a closer look at the workflow token. The workflow token is the execution of a given workflow.

Getting ready

We don't need anything special, just the ability to create a new workflow.

How to do it...

In this example, we start a workflow asynchronously and then wait for it to finish the workflow token:

  1. Create a new workflow and create the following variables:

    Name

    Type

    Section

    Use

    sleepTime

    Number

    IN

    It defines the time the workflow should sleep.

    wfToken

    WorkflowToken

    Attribute

    The workflow token of the asynchronous workflow.

    waitTaskTime

    Number

    Attribute

    How long to wait between checks.

  2. Build the following workflow (06.07.01 Wait for Workflow Token in the example package):

    How to do it...

  3. The custom decision element should contain the following script:
          if (wfToken.state =="completed"){ 
             return true; 
          } else { 
             return false; 
          } 
    
  4. Save and run the workflow

How it works...

The workflow token is a unique ID that is created with each workflow run. It contains a link to all the content of a workflow run, such as the logs, as well as all the attributes and in-and out parameters. You can access it via JavaScript, the API (see the recipe Accessing Orchestrator REST API in Chapter 7, Interacting with Orchestrator) or the Control Center (see the recipe Control Center titbits in Chapter 2, Optimizing Orchestrator Configuration). When you look at a workflow run (the items underneath a workflow), you see nothing else but the Orchestrator Client accessing the data stored in the workflow token.

In addition to the properties, there are also some methods that allow you to cancel or answer the workflow.

Each workflow token has simple properties and methods, and they are as follows:

Properties and methods

Description

.id

Converts the token ID into a string.

.startDate

This is the date and time the workflow was started.

.endDate

This is the date and time the workflow was finished. Null means that it is still running.

.state

This is the state the workflow is in. The different states are waiting, failed, completed, cancelled, and running.

.name

This is the name of the workflow.

.exception

This is the error message a workflow generated and is Null if no error occurs.

.getAttributes()

This helps get all the attributes of the workflow; it returns an Orchestrator properties object. The return is a JavaScript object.

.getInputParameters()

This helps get all the in-parameters of the workflow; it returns an Orchestrator properties object. The return is a JavaScript object.

.getOutputParameters()

This helps get all the out-parameters of the workflow; it returns an Orchestrator properties object. The return is a JavaScript object

.cancel()

Cancels the workflow execution.

.saveSchemaImageToFile(file)

Saves the image of the workflow schema to a file.

Server.getWorkflowTokenState(String_token_id)

Gets the state of a given String_token_id.

See also

See the example workflow 06.06.04 Wait for Workflow Token.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset