Scriplets

Descope flows offer various steps, including screen building, actions, adding connectors, and conditions. Those steps may offer a certain level of customization, such as creating an input field or adding a custom error-handling step. By definition, those steps have a pre-defined output that Descope exposes to the flow context.

On certain occasions, a more flexible, developer-oriented approach is needed to perform mathematical, algorithmic, or just more convenient actions. Descope offers a way to do so with an action called "Scriptlet." This article will cover how to use the action with actual use cases.

scriplet action

Using Scriptlet

An action called "Scriptlet" can be added inside the flow editor.

Arguments

It's crucial to provide arguments as they allow passing the variables necessary to execute the action within the scriptlet. The options available are:

  • Dynamic - a value taken from the context.
  • String / Boolean / Number / Time - static value set in the input, each validated accordingly.

scriplet arguments

In the example above, we use the scriptlet to populate the "greeting" key with "Hello, x", which takes the "John" value if the "form.displayName" context key is empty.

Abilities

The scriptlet code block embedds the following libraries that allow performing more advanced actions:

Each library provides crucial abilities that allow manipulating the arguments, such as Lodash's iteration functions or CryptoJS's hashing functions.

Context Key

The result will be populated to the context key provided.

scriplet context key

In this example, the output will look like this:

  "scripts": {
    "scriptletResult": {
      "greeting": "Hello, x!"
    }
  }

Overrides

Scriptlets can be leveraged to override existing keys in the flow context, like form.displayName, form.customAttributes, etc.

scriplet override

For instance, if your Scriptlet code strips email subaddresses out and returns to the email key, which is accessible via scripts.scriptletResult(or whatever custom key you have), like the below:

let result = email;
const domainIndex = email.lastIndexOf('@');
if (domainIndex > -1) {
  // determine if email is a subaddress per any existing '+' other than the first character
  const aliasIndex = email.substring(1).indexOf('+') + 1;
  if (aliasIndex > 0) {
    result = email.substring(0, aliasIndex) + email.substring(domainIndex)
  }
}
 
return {
  email: result,
};

Then, you can use the Overrides section to set whatever flow context field you'd like (eg. form.email) to the newly returned value (eg. scripts.scriptletResult.email).

Test

Using the test functionality allows you to provide a dummy context input to test out the script's result: scriplet test functionality

Use Cases

In this section we will show real use cases that can be used with Descope flows.

Hashing

In this scenario, the Descoper wants to create a new tenant if the user is new.

scriplet scenario 1, part1

The user's tenant ID will be generated in a unique way using the email domain of the user:

scriplet scenario 1, part 2

Eventually, we will use the result in the Create Tenant action: scriplet scenario 1, part 3

Time-based Condition

In this scenario, the Descoper checks if the user haven't logged in in the last 30 days. If not, the end user will be asked to perform MFA:

scriplet scenario 2, part 1

We will use the scriptlet to take the last authentication time and compare it to now:

scriplet scenario 2, part 2

Now, we will use the output inside a condition:

scriplet scenario 2, part 3

Was this helpful?

On this page