---
schema_version: "1"
id: 411826944c65f56dd0963bd451421b81434fdcda97171ad404b89a46929b62b4
title: Functions for Rules
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/ind/264-0-0/comms_functions_for_rules.md
canonical_url: https://help.salesforce.com/s/articleView?id=ind.comms_functions_for_rules.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 3bbce8b09bcc2851b7459bf21e31ab98d40f6d8742cf817ab79a7cd50e8200b9
description: In context rules, when you need rules to evaluate complex criteria, you can create a function.
products:
  - Communications
  - Media
  - Energy and Utilities
release: "264"
---
# Functions for Rules

In context rules, when you need rules to evaluate complex criteria, you can create a function.

You might want to count records, looking for a certain threshold because, for example, a customer with more than two mobile phone assets is eligible for the family data plan promotion. Or, you might want to know if a certain type of record exists in a data set, returned as a Boolean value (true/false); for example, has the account participated in a campaign in the last 12 months? Because information like this does not exist as a single field on an object, functions enable you to insert custom logic to create advanced rule criteria. Creating rules using functions allows complete flexibility and control, but also requires Apex code.

Functions invoke Apex classes and methods. When you create the function, you can define multiple input arguments and a single output argument that will be passed to and from the Apex class and method. Over time, you can build a library of functions that can be reused for a variety of different purposes and for different context rules.

**Functions**

![Figure is described in the preceding text.](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-ind-comms_media_energy_managed-264-0-0-production-enus/ff4079f6-a384-475c-b863-578bbbb0ef9b/resources-product/images/functions.jpg)

Functions are used in context mappings or in rule conditions. When functions are used in context mappings, the function output becomes the context mapping value and is compared by the rules engine to the rule condition values, which are set by the context dimension, just like when you create a context mapping to a field on an object.

However, when functions are used in rule conditions, the output from the function is compared to a static value in the rule condition. The other context rule components (such as context dimension) can be used as input arguments.

In some cases, you might want to create a context rule to evaluate the product or promotion that the context rule has been applied to.

For example, you might want to prevent customers who have used a promotion from using it again. To do this, you can create a function that uses the [CurrentRuleObjectId variable](https://help.salesforce.com/s/articleView?id=ind.comms_currentruleobjectid_variable.htm&release=264.0.0&type=5 "In some cases, you might want to create a context rule that evaluates the entity to which it is applied. For example, you might want to create a rule that disqualifies a promotion for accounts that have already used that promotion. Or perhaps you want a rule to disqualify products when they are already in the cart line items of Industries Cart. To do this, you use a special variable called CurrentRuleObjectId, which enables your rule conditions or functions to access the current object's record Id as it iterates over them.") as an input argument.

## Creating Functions

You can create a custom function from an Apex class and use the function in rules.

1. Create your Apex class and note the Apex class name and the method name. For more information on working with Apex classes, see Salesforce's [Apex Developer Guide](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dev_guide.htm).
2. Create a function definition in Custom Metadata.
3. In Setup, go to Custom Metadata Types.
4. Click **Manage Function Definitions**.
5. In Function Definitions, Click **New**.
6. Enter the following information:
    
    <table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small ">
                <colgroup>
                  <col>
                  <col>
                  <col>
                </colgroup>
                <thead class="slds-text-title_caps ">
                  <tr class="">
                    <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d334178e162">
                      <p style="margin-bottom:0;">Label</p>
                    </th>
                    <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d334178e168">
                      <p style="margin-bottom:0;">Value</p>
                    </th>
                    <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d334178e174">
                      <p style="margin-bottom:0;">Description</p>
                    </th>
                  </tr>
                </thead>
                <tbody>
                  <tr class="">
                    <td data-label="Label" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e162 ">
                      <p style="margin-bottom:0;">Name</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e168 ">
                      <p style="margin-bottom:0;">String value</p>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e174 ">
                      <p style="margin-bottom:0;">The function name will be displayed in the list on the Functions Definitions tab.</p>
                    </td>

                  </tr>
                  <tr class="">
                    <td data-label="Label" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e162 ">
                      <p style="margin-bottom:0;">Function Definition</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e168 ">
                      <p style="margin-bottom:0;">String value</p>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e174 ">
                      <p style="margin-bottom:0;">The unique function name used by the API and managed packages, and which you use in formulas. The name must begin with a letter and use only alphanumeric characters and underscores. The name cannot end with an underscore or have two consecutive underscores.</p>
                    </td>

                  </tr>
                  <tr class="">
                    <td data-label="Label" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e162 ">
                      <p style="margin-bottom:0;">Class Name</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e168 ">
                      <p style="margin-bottom:0;">String value</p>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e174 ">
                      <p style="margin-bottom:0;">The Apex class containing the method that implements the logic for the function.</p>
                    </td>

                  </tr>
                  <tr class="">
                    <td data-label="Label" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e162 ">
                      <p style="margin-bottom:0;">Method Name</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e168 ">
                      <p style="margin-bottom:0;">String value</p>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e174 ">
                      <p style="margin-bottom:0;">The name of the method in your Apex class that implements the logic for the function.</p>
                    </td>

                  </tr>
                </tbody>
              </table>
7. Create a function.
    
    - In Vlocity Product Console under **Rules**, click the create icon next to **Function**.
    - In the **New Function** dialog, enter the following information:
        
        <table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small ">
                    <colgroup>
                      <col>
                      <col>
                      <col>
                    </colgroup>
                    <thead class="slds-text-title_caps ">
                      <tr class="">
                        <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d334178e313">
                          <p style="margin-bottom:0;">Property</p>
                        </th>
                        <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d334178e319">
                          <p style="margin-bottom:0;">Values</p>
                        </th>
                        <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d334178e325">
                          <p style="margin-bottom:0;">Description</p>
                        </th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr class="">
                        <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e313 ">
                          <p style="margin-bottom:0;">Name</p>
                        </td>

                        <td data-label="Values" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e319 ">
                          <p style="margin-bottom:0;">String value</p>
                        </td>

                        <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e325 ">
                          <p style="margin-bottom:0;">The name of the function. You cannot use spaces in the name of a function due to requirements from the rule parsing engine.</p>
                        </td>

                      </tr>
                      <tr class="">
                        <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e313 ">
                          <p style="margin-bottom:0;">Code</p>
                        </td>

                        <td data-label="Values" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e319 ">
                          <p style="margin-bottom:0;">String value</p>
                        </td>

                        <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e325 ">
                          <p style="margin-bottom:0;">The code of the function.</p>
                        </td>

                      </tr>
                      <tr class="">
                        <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e313 ">
                          <p style="margin-bottom:0;">Description</p>
                        </td>

                        <td data-label="Values" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e319 ">
                          <p style="margin-bottom:0;">String value</p>
                        </td>

                        <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e325 ">
                          <p style="margin-bottom:0;">Describe the purpose of the function. It's also helpful to note the inputs and output.</p>
                        </td>

                      </tr>
                      <tr class="">
                        <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e313 ">
                          <p style="margin-bottom:0;">Implementation Name</p>
                        </td>

                        <td data-label="Values" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e319 ">
                          <p style="margin-bottom:0;">String value</p>
                        </td>

                        <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e325 ">
                          <p style="margin-bottom:0;">The name of the method in your Apex class that implements the logic for the function.</p>
                        </td>

                      </tr>
                      <tr class="">
                        <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e313 ">
                          <p style="margin-bottom:0;">Output Mode</p>
                        </td>

                        <td data-label="Values" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e319 ">
                          <p style="margin-bottom:0;">Single</p>
                        </td>

                        <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e325 ">
                          <p>Used to specify whether the output will be a single value or multiple values.</p>
                          <p style="margin-bottom:0;">Currently, only single output values are supported.</p>
                        </td>

                      </tr>
                      <tr class="">
                        <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e313 ">
                          <p style="margin-bottom:0;">Active</p>
                        </td>

                        <td data-label="Values" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e319 ">
                          <p style="margin-bottom:0;">Click to select</p>
                        </td>

                        <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e325 ">
                          <p style="margin-bottom:0;">Must set as active to be used at runtime.</p>
                        </td>

                      </tr>
                      <tr class="">
                        <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e313 ">
                          <p style="margin-bottom:0;">Effective From Date</p>
                        </td>

                        <td data-label="Values" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e319 ">
                          <p style="margin-bottom:0;">Select a date</p>
                        </td>

                        <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e325 ">
                          <p style="margin-bottom:0;">The start date of the period during which the function will be effective.</p>
                        </td>

                      </tr>
                      <tr class="">
                        <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e313 ">
                          <p style="margin-bottom:0;">Effective Until Date</p>
                        </td>

                        <td data-label="Values" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e319 ">
                          <p style="margin-bottom:0;">Select a date</p>
                        </td>

                        <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e325 ">
                          <p style="margin-bottom:0;">The end date of the period during which the function will be effective.</p>
                        </td>

                      </tr>
                    </tbody>
                  </table>
8. Click **Save**.
9. Create function arguments.
    
    ![Note](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-ind-comms_media_energy_managed-264-0-0-production-enus/ff4079f6-a384-475c-b863-578bbbb0ef9b/images/icon_note.png)
    
    Note
    
    You can create multiple input arguments but a single output argument. The names of the arguments are not passed to the Apex class. Instead, the sequence of the input arguments is used to identify each argument within the Apex code. After the argument sequence has been set, do not change it without editing the corresponding Apex class accordingly.
10. In the left sidebar, click **Function Arguments**.
11. In the **Function Arguments** facet, click **New Argument**.
12. In the **General Properties** window, enter the following information:
    
    <table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small ">
                <colgroup>
                  <col>
                  <col>
                  <col>
                </colgroup>
                <thead class="slds-text-title_caps ">
                  <tr class="">
                    <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d334178e581">
                      <p style="margin-bottom:0;">Property</p>
                    </th>
                    <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d334178e587">
                      <p style="margin-bottom:0;">Value</p>
                    </th>
                    <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d334178e593">
                      <p style="margin-bottom:0;">Description</p>
                    </th>
                  </tr>
                </thead>
                <tbody>
                  <tr class="">
                    <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e581 ">
                      <p style="margin-bottom:0;">Sequence</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e587 ">
                      <p style="margin-bottom:0;">Numeric value</p>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e593 ">
                      <p style="margin-bottom:0;">The sequence of the argument. Salesforce recommends using a 10, 20, 30 ... numbering sequence to allow for easy insertions in future. The sequence specified for each argument must match the sequence of arguments expected by the Apex class specified in function's <strong class="uicontrol">Implementation Name</strong> property.</p>
                    </td>

                  </tr>
                  <tr class="">
                    <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e581 ">
                      <p style="margin-bottom:0;">Argument Name</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e587 ">
                      <p style="margin-bottom:0;">String value</p>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e593 ">
                      <p>The name of the argument.</p>
                      <p style="margin-bottom:0;">Function argument names cannot contain spaces.</p>
                    </td>

                  </tr>
                  <tr class="">
                    <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e581 ">
                      <p style="margin-bottom:0;">Direction</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e587 ">
                      <ul>
                        <li>
                          <p>
                            <kbd>Input</kbd>
                          </p>
                        </li>

                        <li>
                          <p>
                            <kbd>Output</kbd>
                          </p>
                        </li>

                      </ul>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e593 ">
                      <p><kbd>Input</kbd> specifies the argument that will be passed to the function as input. You can define multiple input arguments.</p>
                      <p style="margin-bottom:0;"><kbd>Output</kbd> specifies the argument that will be passed to the function as output. You can only define one output arguments.</p>
                    </td>

                  </tr>
                  <tr class="">
                    <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e581 ">
                      <p style="margin-bottom:0;">Data Type</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e587 ">
                      <ul>
                        <li>
                          <p>
                            <kbd>Text</kbd>
                          </p>
                        </li>

                        <li>
                          <p>
                            <kbd>Number</kbd>
                          </p>
                        </li>

                        <li>
                          <p>
                            <kbd>Date</kbd>
                          </p>
                        </li>

                        <li>
                          <p>
                            <kbd>DateTime</kbd>
                          </p>
                        </li>

                        <li>
                          <p>
                            <kbd>Boolean</kbd>
                          </p>
                        </li>

                      </ul>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e593 ">
                      <p style="margin-bottom:0;">Specifies the data type of the argument.</p>
                    </td>

                  </tr>
                  <tr class="">
                    <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e581 ">
                      <p style="margin-bottom:0;">Domain Type</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e587 ">
                      <ul>
                        <li>
                          <p>
                            <kbd>Picklist</kbd>
                          </p>
                        </li>

                        <li>
                          <p>
                            <kbd>Object Lookup</kbd>
                          </p>
                        </li>

                        <li>
                          <p>
                            <kbd>Type in</kbd>
                          </p>
                        </li>

                        <li>
                          <p>
                            <kbd>Context Dimension</kbd>
                          </p>
                        </li>

                      </ul>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e593 ">
                      <p>Specifies the domain type of the input argument.</p>
                      <ul>
                        <li>
                          <p>If you select <kbd>Picklist</kbd> and specify a Vlocity Picklist here, the picklist values are displayed next to the <strong class="uicontrol">Picklist</strong> radio button when you create a rule condition or a context mapping later.</p>
                        </li>

                        <li>
                          <p>If you select <kbd>Object Lookup</kbd> and then specify an object in the <strong class="uicontrol">Object</strong> field here, you can select a record from that object when you create a rule condition or a context mapping later.</p>
                        </li>

                        <li>
                          <p>If you select <kbd>Type in</kbd>, you can type in a value when you create a rule condition or a context mapping later.</p>
                        </li>

                        <li>
                          <p>If you select <kbd>Context Dimension</kbd>, you can specify a context dimension to pass as input to the function when you create a rule condition or a context mapping later.</p>
                        </li>

                      </ul>
                      <p style="margin-bottom:0;">Select <kbd>Context Dimension</kbd> as the Domain Type when using the CurrentRuleObjectId variable.</p>
                    </td>

                  </tr>
                  <tr class="">
                    <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e581 ">
                      <p style="margin-bottom:0;">Object</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e587 ">
                      <p style="margin-bottom:0;">String value</p>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e593 ">
                      <p style="margin-bottom:0;">Used with Domain Type of Object Lookup. Specifies the API name of the sObject to lookup.</p>
                    </td>

                  </tr>
                  <tr class="">
                    <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e581 ">
                      <p style="margin-bottom:0;">Vlocity Picklist</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e587 ">
                      <p style="margin-bottom:0;">Select a picklist using the <strong class="uicontrol">Vlocity Picklist Lookup</strong> dialog.</p>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e593 ">
                      <p style="margin-bottom:0;">Used with Domain Type of Picklist. Specifies the picklist to use as input to the function.</p>
                    </td>

                  </tr>
                  <tr class="">
                    <td data-label="Property" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e581 ">
                      <p style="margin-bottom:0;">Picklist Selection Mode</p>
                    </td>

                    <td data-label="Value" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e587 ">
                      <ul>
                        <li>
                          <p>
                            <kbd>Single</kbd>
                          </p>
                        </li>

                        <li>
                          <p>
                            <kbd>Multiple</kbd>
                          </p>
                        </li>

                      </ul>
                    </td>

                    <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d334178e593 ">
                      <p style="margin-bottom:0;">Used with domain type of picklist. Specifies whether the user is allowed to pick one value from the picklist or multiple values.</p>
                    </td>

                  </tr>
                </tbody>
              </table>
13. Click **Save**.
    
    Add as many arguments as needed to support the function's implementation class, then create the context mapping or rule condition that uses the function.