Create a Constant Table ​
Constant tables are useful for organizing somewhat complex static information in your Ruleset.
Procedure ​
It's easy to get started:
- Create a new Constant named Levels.
- Change the Constant type from Value to Table.
Now, let's add some data.
- Change the key name to Low.
- Change the value to 0.
- Press Enter.
- Enter Medium
- Press Tab.
- Enter 50.
- Press Enter.
- Enter High and 100 for the key and value, respectively.
You should now end up with a table with the following structure:
| Key | value |
|---|---|
| Low | 0 |
| Medium | 50 |
| High | 100 |
Using your Constant table ​
Let's use the Levels constant table in a rule.
- Create a new Rule named LevelTest
- In Expression type:
Levels.
Notice how it tries to auto-complete with Low, Medium, High and includes. We'll cover includes in a moment.
- Change the Expression to:
Levels.Medium. - Save the Ruleset.
- Scroll to the top and click on TRY IT OUT!
Let's check what LevelTest evaluates to.
- Click on EXECUTE.
If all is well, it should return 50 for LevelTest.
Expanding the table with extra values ​
A constant table can include more than one value per key. Let's expand our Levels table with ranges!
- Open the Levels constant again.
- Click on ADD VALUE.
- Change value into from.
- Change value2 into to.
- Add values to the to column so the table matches this:
| Key | from | to |
|---|---|---|
| Low | 0 | 49 |
| Medium | 50 | 99 |
| High | 100 | 999 |
Now, using this constant table is quite simple.
- Go back to the LevelTest rule.
- Change the Expression to:
Levels.Medium.from - Save the Ruleset and return to the Sandbox.
- Execute the Ruleset again.
Once again, you should see 50 as the value for LevelTest.
Instead of using 50 and 99 directly in our code, we can use them by writing Levels.Medium.from and Levels.Medium.to. For instance, we may have a rule that checks if a value is "Low", "Medium" or "High".
- Add a new Input named
value - Create 3 rules named IsLow, IsMedium, and IsHigh with the following expressions:
- IsLow:
value >= Levels.Low.from && value <= Levels.Low.to - IsMedium:
value >= Levels.Medium.from && value <= Levels.Medium.to - IsHigh:
value >= Levels.High.from && value <= Levels.High.to
- IsLow:
- Try the ruleset out in the Sandbox with different values for
value.