Macros in Rules

Use a macro when a single rule executes repeatedly on different JSON node paths.

Rule macros enable template developers to define repeatable, well-tested rule code units that can be called within the context of any JSON transformation, simplifying and making the rule definition source easier to maintain.

You invoke macros using standard function syntax. Reference them anywhere a traditional expression is resolved within a rule action. In template rules, macros can be referenced by an ‘eval’ action. Macros can include actions and can reference other macros, and macro recursion is supported. Template support a macro depth of 10; beyond that results in an overflow error and app creation failure.

For example, consider the following macro for multiplying two numbers:

1{
2           "namespace": "testMacroNamespace",
3           "definitions": [
4               {
5                   "name": "multiplyTwoNumbers",
6                   "parameters": [
7                       "firstNumber",
8                       "secondNumber"
9                   ],
10                   "returns": "${p.firstNumber * p.secondNumber}"
11               },
12               {
13                   "name": "shouldExecuteAction",
14                   "returns": true
15               }
16           ]
17}

It can be invoked in rules actions as follows:

1{
2    "rules": [
3        {
4            "name": "Testing of example macro",
5            "actions": [
6                {
7                    "action": "set",
8                    "key": "macroResult",
9                    "path": "$.path.to.a.number",
10                    "value": "${testMacroNamespace:multiplyTwoNumbers(3,3)}"
11                },
12                {
13                    "action": "put",
14                    "condition": "${testMacroNamespace:shouldExecuteAction()}",
15                    "path": "$.my.json.path",
16                    "value": {
17                        "foo": "bar",
18                        "anotherNumber": “${testMacroNamespace:multiplyTwoNumbers(5,5)}”
19                    }
20                }
21            ]
22        }
23    ]
24}