Create Simple Scripting Rule
This tutorial will walk you through the creation of a simple Scripting Rule.
Last updated
This tutorial will walk you through the creation of a simple Scripting Rule.
Last updated
Let's advance one step at a time.
Becoming a superhero is a fairly straightforward process. After entering our login page, you will be able to pass your credentials and log in.
There are multiple options for user login. If you do not have an account yet, you can create one. After logging in to the application, the Dashboard will be displayed.
You will be prompted to provide a name and choose between SAMPLE RULE or EMPTY RULE. For now, name the rule as you wish and choose the EMPTY RULE. The new rule will be created and its detail will be displayed. We will continue in the Rule Settings tab.
When you click on RULE SETTINGS on the top left corner, the scripting rule's detail will appear first to set some information. We will change the name of our script. To do this, click on it's name, enter one you like and press Enter.
Since we do not want this decision table to be available yet, we will change its status to "Pending". To do this, click on the current status "Published" and then select "Pending".
We will now create an input and output model, which we will then use to set conditions and results. We create this model with a JSON editor.
First, we delete all created objects. Then we will add our specified requirements (value1, value2) as empty objects.
Because our model is simple, these objects do not contain any others. For more complex models, more information is here.
We set the output model similarly, where we set it as root result (empty object).
Output model Example:
Now let's move on to code editor by clicking on Scripting Rule Designer it in the right upper corner and create individual rules.
Our code editor is based on Monaco Editor, using its features, like autocomplete, syntax highlight, line numbers, etc.
Shortcut Keys are also working, but you need to be with a cursor in the editor.
CTRL/CMD + S - save
CTRL/CMD + R - run
CTRL/CMD + Z - undo
CTRL/CMD + SHIFT + Z - redo
CTRL/CMD + F - find
SHIFT + ALT + F - format
Scripts must be written in JavaScript language.
For simplicity, we will remove the code from the code editor to create a new rule.
When to code editor is empty, we can start to create our own rule in JavaScript. It is straightforward, and you need to write your code which can look like below.
Input must always be entered as input.yourInputVariable.
Output must always be entered as output.yourOutputVariable.
To return an output, always enter return output at the end of your script!
console.log() is forbidden due to performance, but you can use log() instead.
You can use log() to print values in the console, which is at the bottom of the code editor.
Don't forget to save your scripting rule!
Now we can test our rule. Before testing the rule, we must change the status of the rule to "Published".
You can find more information about input and result at Solver API.
More information about Test Bench is here.
To display the rule creation pop-up click the button on the sidebar.
After creating an input or output model, we must always confirm the changes with the button.
Always save your script using (bottom of the page) or CTRL/CMD + S
If we want to test a rule, we can click on the icon at the bottom of the page. An input and output window will show up at the bottom of the page.
We can either click on the run button between input and output windows or the button on the top of the code editor. The result will be displayed in the Output window (the right one).
The debug mode can be turned on by clicking on . In scripting rules, it will enable to write log() in the console.