Expressions

JavaScript expressions

When working with Elsa, you'll often need to write dynamic expressions. This page provides a glossary of JavaScript expressions that you can use.

Elsa uses the Jint library to implement JavaScript. More functions can be found there, as well as details on providing your own functions.

Common

These are built-in JS functions that are common to most JavaScript implementations.

FunctionDescriptionExample
JSON.parse(Object)Parse a JavaScript object. Also works with ExpandoObject objects.JSON.parse('{"name": "Alice"}')
JSON.stringify(string)Stringify a JavaScript object. Only works with JSON objects (which are ExpandoObjects in Jint). Use toJson() instead if you need to serialize .NET objects other than ExpandoObjectJSON.stringify({ name: "Alice" })
parseInt(string): numberParses the specified string into a number.parseInt('42')

Activity output

The following functions are specific to handling activity output.

FunctionDescriptionExample
getOutputFrom(string, string?): anyGets the output of the activity by activity name or ID.getOutputFrom('HttpEndpoint1')
get{OutputName}From{ActivityName}(): anyGets the output of the activity by activity name.getParsedContentFromHttpEndpoint1()
getLastResult(): anyGets the last result.getLastResult()

Workflow variables and input

The following functions are specific to handling workflow variables and input.

FunctionDescriptionExample
getVariable(string): anyGets a variable from the workflow.getVariable('MyVariable')
setVariable(string, any)Sets a variable in the workflow.setVariable('MyVariable', 'myValue')
get{VariableName}(): anyGets a variable from the workflow.getMyVariable()
set{VariableName}(any)Sets a variable in the workflow.setMyVariable('MyValue')
getInput(string): anyGets the input of the workflow.getInput('name')

Workflow variables

There are two ways to get variables from the workflow:

  • getVariable('MyVariable')
  • getMyVariable()

The first method is useful when you want to get a variable whose name is not known at build-time. The second method is useful when you know the name of the variable at build-time, which has the added benefit of providing intellisense.

Similarly, there are two ways to set variables in the workflow:

  • setVariable('MyVariable', 'myValue')
  • setMyVariable('myValue')

Workflow input

To get the input of the workflow, use the getInput() function.

For example, if you run a workflow providing the following input:

{
  "input": {
    "name": "Alice"
  }
}

You can get the name property using the following expression:

getInput('name')

Workflow

These functions are specific to working with workflows.

FunctionDescriptionExample
getWorkflowInstanceId(): stringGets the ID of the workflow instance.getWorkflowInstanceId()
getCorrelationId(): stringGets the correlation ID of the workflow instance.getCorrelationId()
setCorrelationId(string)Sets the correlation ID of the workflow instance.setCorrelationId('myCorrelationId')
isNullOrWhiteSpace(string): booleanReturns true if the string is null, empty, or consists only of white-space characters.isNullOrWhiteSpace('')
isNullOrEmpty(string): booleanReturns true if the string is null or empty.isNullOrEmpty('')
parseGuid(string): GuidParses a string into a GUID.parseGuid('00000000-0000-0000-0000-000000000000')
newGuid(): GuidGenerates a new GUID.newGuid()
newGuidString(): stringGenerates a new GUID and returns it as a string.newGuidString()
newShortGuid(): stringGenerates a new GUID and returns it as a short string.newShortGuid()
toJson(any)Converts an object to JSON. Use this instead of JSON.stringify() when you need to serialize a .NET object to JSON. JSON.stringify() only works on ExpandObject objects.toJson('{"name": "Alice"}')

Utility

These functions are useful for working with strings, numbers, GUIDs, collections and more.

FunctionDescriptionExample
isNullOrWhiteSpace(string): booleanReturns true if the string is null, empty, or consists only of white-space characters.isNullOrWhiteSpace('')
isNullOrEmpty(string): booleanReturns true if the string is null or empty.isNullOrEmpty('')
parseGuid(string): GuidParses a string into a GUID.parseGuid('00000000-0000-0000-0000-000000000000')
newGuid(): GuidGenerates a new GUID.newGuid()
newGuidString(): stringGenerates a new GUID and returns it as a string.newGuidString()
newShortGuid(): stringGenerates a new GUID and returns it as a short string.newShortGuid()
toJson(any)Converts an object to JSON. Use this instead of JSON.stringify() when you need to serialize a .NET object to JSON. JSON.stringify() only works on ExpandObject objects.toJson('{"name": "Alice"}')
Previous
C#