Function format
This article describes the format of validation and formula functions, such as function syntax, parameters, and return values.
Coding language
Custom logic functions are JavaScript (ES11) code snippets that perform data validations or calculations on specific object records. Each function is an Immediately Invoked Function Expression (IIFE) linking to a particular object.
For more information about JavaScript and JavaScript functions, see JavaScript and Functions in MDN Web Docs.
Format of validation functions
You can use validation functions to define validation rules on object fields to ensure data accuracy and consistency.
Zuora calls validation functions on the creation or update of a specific object record and uses the record as the only parameter.
The fields that can be used in validation functions vary depending on the object type. For more information, see Objects and fields supported by Custom Logic.
Syntax
((object_record) => { //JavaScript code(validation rules) })(object_record)
Parameters
The function takes one parameter object_record
. This parameter is required and the parameter name is not editable. The parameter name varies depending on the related object type.
The following table lists the parameter names for some common standard and custom objects:
Object name | Parameter name |
---|---|
Account | account |
Product | product |
Vehicle (custom object) | default__vehicle |
When a function is called, Zuora provides the context object record as the input value. For example, if the function is called during the creation of an account object record, the parameter name is account
, which refers to this specific account object record.
Return value
The return value is an object of the following format:
{ success: boolean, message?: string }
Key | Required | Value format | Description |
---|---|---|---|
success | Yes | Boolean | Indicating the running result of the function:
|
message | No | String | The message displayed when the data evaluation fails. |
Example
((account) => { if (account.Country__c === 'United States' && !account.BillingState__c) { return { success: false, message: "Billing State is required for accounts in the United States." }; } return { success: true }; })(account);
In this example, the function checks whether the BillingState__c
custom field is specified when the value in the Country__c
custom field is United States
.
If success
is true
in the returned object, Zuora will create or update the account object record by using the data from account
. If success
is false
, Zuora will display the following error message and not create or update the account object record.
Billing State is required for accounts in the United States.
With the help of this validation function, you can ensure the BillingState__c
custom field is conditionally required every time an account object record is created or updated.
Format of formula functions
You can use formula functions to automate calculations and update object fields.
Zuora calls formula functions on the creation or update of a specific object record and uses the record as the only parameter.
The fields that can be used in formula functions vary depending on the object type. For more information, see Objects and fields supported by Custom Logic.
Syntax
((object_record) => { //JavaScript code(calculations) })(object_record)
Parameters
The function takes one parameter object_record
. This parameter is required and the parameter name is not editable. The parameter name varies depending on the related object type.
The following table lists the parameter names for some common standard and custom objects:
Object name | Parameter name |
---|---|
Account | account |
Product | product |
Vehicle (custom object) | default__vehicle |
When a function is called, Zuora provides the context object record as the input value. For example, if the function is called during the creation of an account object record, the parameter name is account
, which refers to this specific account object record.
Return value
The return value is an object of the following format:
{ success: boolean, message?: string, data?: object }
Key | Required | Value format | Description |
---|---|---|---|
success | Yes | Boolean | Indicating the running result of the function:
|
message | No | String | The message displayed when the data calculation fails. |
data | No | Object |
The updated fields in the format of key-value pairs. For example: { Year__c: 2023, Country__c: 'United States' } This attribute is available only if Zuora ignores the fields that do not exist on the object. |
Example
((default__vehicle) => { const purchaseAmount = default__vehicle.PurchaseAmount__c; const taxRate = default__vehicle.TaxRate__c; const salesTax = purchaseAmount * taxRate; return { success: true, message: "The sales tax is updated.", data: { SalesTax__c: salesTax } }; })(default__vehicle)
In this example, the function multiplies the purchase amount in the PurchaseAmount__c
custom field by the tax rate in the TaxRate__c
custom field, and then updates the SalesTax__c
custom field with the calculation result.
With the help of this formula function, you can automate sales tax calculations every time a vehicle object record is created or updated without the need to calculate it manually.
Best practices for using functions
It is recommended to follow these best practices when using functions.
Use conditional statements in validation functions
You should use conditional statements (for example, if-else
) in validation functions to specify validation rules.
Return only fields that need to be updated in formula functions
It is best practice to return only the fields that need to be updated in formula functions. Zuora ignores the returned fields that are not defined on the source object.