---
schema_version: "1"
id: 8775f7c697d4df38489d280c8d3b619dcf6cfd8de1e13ef8ec3910ef548b0095
title: Macros in Rules
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_wave_templates/guides/concept/bi-templatesdev-macros-in-rules.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-wave-templates/guide/bi-templatesdev-macros-in-rules.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: b53a63910711e933d1fe64f9e446e8fd9f87303e76795580e35c8110d1d1fda2
products:
  - Analytics Templates Developer Guide
  - analytics
---

# 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:

```nolang
{
           "namespace": "testMacroNamespace",
           "definitions": [
               {
                   "name": "multiplyTwoNumbers",
                   "parameters": [
                       "firstNumber",
                       "secondNumber"
                   ],
                   "returns": "${p.firstNumber * p.secondNumber}"
               },
               {
                   "name": "shouldExecuteAction",
                   "returns": true
               }
           ]
}
```

It can be invoked in rules actions as follows:

```nolang
{
    "rules": [
        {
            "name": "Testing of example macro",
            "actions": [
                {
                    "action": "set",
                    "key": "macroResult",
                    "path": "$.path.to.a.number",
                    "value": "${testMacroNamespace:multiplyTwoNumbers(3,3)}"
                },
                {
                    "action": "put",
                    "condition": "${testMacroNamespace:shouldExecuteAction()}",
                    "path": "$.my.json.path",
                    "value": {
                        "foo": "bar",
                        "anotherNumber": “${testMacroNamespace:multiplyTwoNumbers(5,5)}”
                    }
                }
            ]
        }
    ]
}
```

- **[Rule Macro And Macro Definition Attributes](https://developer.salesforce.com/docs/analytics/bi-dev-guide-wave-templates/guide/bi-templatesdev-rulemacro-attributes.md)**


    A rule macro object contains a collection of macro definition objects. Macro definition objects define the logic of macros and their return values.

- **[Macro Examples](https://developer.salesforce.com/docs/analytics/bi-dev-guide-wave-templates/guide/bi-templates-dev-macro-examples.md)**


    The following examples show the structure and syntax of macros in rules.

- **[Macro Use Case: Recursive Operations](https://developer.salesforce.com/docs/analytics/bi-dev-guide-wave-templates/reference/bi-templatesdev-rules-macros-sample-recursive.md)**


    Here’s an example of a macro that deletes an `sfdcDigest` node. It also deletes the digest node and all other nodes that depend on that node.

- **[Macro Use Case: Delete Workflow Nodes](https://developer.salesforce.com/docs/analytics/bi-dev-guide-wave-templates/reference/bi-templatesdev-rules-macros-sample-delete.md)**


    Use this macro to delete multiple nodes from the workflow without having to write multiple actions.

- **[Macro Use Case: Add an Array of SObject Names to Extract Workflow](https://developer.salesforce.com/docs/analytics/bi-dev-guide-wave-templates/reference/bi-templatesdev-rules-macros-sample-add-array.md)**


    Here’s a macro that adds an array of `sObject` names to the extract workflow.
