JavaScript

In this section, we cover some of the built-in variables and functions available to the JavaScript expression syntax.

When creating workflows, you'll often need to write dynamic expressions. This page provides an overview of enabling JavaScript expressions and what functions and objects you can use.

Installing the JavaScript Feature

The JavaScript Expressions feature is provided by the following package:

dotnet package add Elsa.JavaScript

You can enable the feature as follows:

Program.cs
services.AddElsa(elsa =>
{
   elsa.UseJavaScript();
});

Configuration

The UseJavaScript extension provides an overload that accepts a delegate that lets you configure JintOptions. These options let you configure the underlying Jint engine.

For example:

Program.cs
services.AddElsa(elsa =>
{
   elsa.UseJavaScript(options =>
   {
      options.AllowClrAccess = true;
      options.RegisterType<Order>();
      options.ConfigureEngine(engine =>
      {
         engine.Execute("function greet(name) { return `Hello ${name}!`; }");
         engine.SetValue("echo", (Func<string, string>)(s => "Echo: " + s));
      });
   });
});

Elsa uses Jint to implement the JavaScript expression evaluator.

Globals

The following functions and objects are available as globals to all JavaScript expressions:

JSON

The JSON type provides static methods to parse JSON strings into JavaScript objects and to serialise JavaScript objects into JSON strings.

// Serialize the specified value as a JSON string.
JSON.stringify(object value);

// Deserialize the specified JSON string into a JavaScript object.
JSON.parse(string json);

variables

The variables object provides static access to the workflow variables. For example, if your workflow has a variable called OrderId, you can get and set that workflow variable using the following JavaScript expression:

// Set the OrderId workflow variable.
variables.OrderId = newGuid();

// Get the OrderId workflow variable.
const orderId = variables.OrderId;

getWorkflowDefinitionId

Returns the workflow definition ID of the currently executing workflow.

getWorkflowDefinitionId(): string;

getWorkflowDefinitionVersionId

Returns the workflow definition version ID of the currently executing workflow.

getWorkflowDefinitionVersionId(): string;

getWorkflowDefinitionVersion

Returns the workflow definition version of the currently executing workflow.

getWorkflowDefinitionVersion(): number;

getWorkflowInstanceId

Returns the workflow instance ID of the currently executing workflow.

getWorkflowInstanceId(): string;

setCorrelationId

Sets the correlation ID of the currently executing workflow to the specified value.

setCorrelationId(value: string);

getCorrelationId

Gets the correlation ID of the currently executing workflow.

getCorrelationId(): string?;

setVariable

Sets the specified workflow variable by name to the specified value.

setVariable(name: string, value: any);

getVariable

Gets the specified workflow variable's value by name.

getVariable(name: string): any;

getInput

Gets the specified workflow input by name.

getInput(name: string): any;

getOutputFrom

Gets the specified activity output by name from the specified activity by name.

getOutputFrom(activityIdOrName: string, outputName?: string): any;

getLastResult

Gets the output of the last activity that executed.

getLastResult(): any;

isNullOrWhiteSpace

Returns true if the specified string is null, empty or consist only of whitespace characters, false otherwise.

isNullOrWhiteSpace(string? value): boolean;

isNullOrEmpty

Returns true if the specified string is null or empty, false otherwise.

isNullOrEmpty(string? value): boolean;

parseGuid

Parses the specified string into a Guid.

parseGuid(string value): Guid;

newGuid

Creates a new Guid.

newGuid(): Guid;

newGuidString

Creates a new Guid as a string representation.

newGuidString(): string;

newShortGuid

Creates a new short GUID as a string representation.

newShortGuid(): string;

bytesToString

Converts a byte array to a string.

bytesToString(buffer: byte[]): string;

bytesFromString

Converts a string to an array of bytes.

bytesFromString(value: string): byte[];

bytesToBase64

Converts a byte array to a base64 string.

bytesToBase64(buffer: byte[]): string;

bytesFromBase64

Converts a base64 string to a byte array.

bytesFromBase64(base64: string): byte[];

stringToBase64

Converts a string to a base64 string.

stringToBase64(value: string): string;

stringFromBase64

Converts a base64 string to a string.

stringFromBase64(base64: string): string;

Last updated