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.
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.
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.
In this example, the output will look like this:
Overrides
Scriptlets can be leveraged to override existing keys in the flow context, like form.displayName
, form.customAttributes
, etc.
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:
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:
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.
The user's tenant ID will be generated in a unique way using the email domain of the user:
Eventually, we will use the result in the Create Tenant
action:
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:
We will use the scriptlet to take the last authentication time and compare it to now:
Now, we will use the output inside a condition: