Use cases and code samples
This article provides common use cases and code samples of validation and formula functions.
Validation function use cases
The following are common use cases of validation functions.
Validate if a product is eligible for delivery to a specific address
The following code sample validates whether the value in the Address__c
field is included in a given array.
((default__vehicle => { const address = default__vehicle.Address__c; if(['30324','30326', '30328', '94702', '94027'].includes(address)){ return { success: true }; } return { success: false, message: "Sorry, we do not deliver to this location" }; })(default__vehicle);
Validate aged invoices
The following code sample validates whether the date in the Due_Date__c
field is more than 90 days ahead of the current date, which indicates the invoice aging is greater than 90 days.
((invoice) => { const dueDate = invoice.Due_Date__c; const currentDate = new Date(); const daysUntilDue = Math.floor((dueDate - currentDate) / (1000 * 60 * 60 * 24)); if (daysUntilDue <= 90) { return { success: true }; } return { success: false, message: "Invoice is overdue and past the 90 days threshold." }; })(invoice);
Validate currency format
The following code sample validates whether the value in the AcceptedCurrency__c
field matches the regular expression for the USD currency format.
((account) => { const currency = account.AcceptedCurrency__c; const currencyFormatRegex = /^\$?\d{1,3}(,\d{3})*(\.\d{1,2})?$/; if (currencyFormatRegex.test(currency)) { return { success: true }; } return { success: false, message: "Currency Value should be a valid USD currency format (e.g., $100,456.03)." }; })(account);
Validate URL protocol
The following code sample validates whether the value in the Source__c
field is a valid URL.
((default__vehicle) => { const urlValue = default__vehicle.Source__c; const url = new URL(urlValue); if (url.protocol === 'http:' || url.protocol === 'https:') { return { success: true }; } return { success: false, message: "Source should have a valid URL format (e.g., http or https.)" }; })(default__vehicle);
Validate field entry
The following code sample validates whether the BillingState__c
field is specified when the value in the Country__c
field is United States
.
((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);
Validate social security number format
The following code sample validates whether the value in the SSN__c
field is a valid social security number.
((default__Credit_Check) => { const ssnValue = default__Credit_Check.SSN__c; const ssnFormatRegex = /^(\d{3}-\d{2}-\d{4}|\d{9})$/; if (ssnFormatRegex.test(ssnValue)) { return { success: true }; } return { success: false, message: "The SSN should have a valid format (###-##-####)." }; })(default__Credit_Check);
Validate positive values
The following code sample validates whether the value in the Credit_Limit__c
field equals or is greater than 0.
((account) => { const creditLimit = account.Credit_Limit__c; if (creditLimit >= 0) { return { success: true }; } return { success: false, message: "Credit limit cannot be negative." }; })(account);
Validate if a field is an email address
The following code sample validates whether the value in the Email__c
field of the default__users
custom object is in the format of an email address.
((default__users) => { const email = default__users.Email__c; const emailFormatRegex = /^([A-Z0-9_+-]+\.?)*[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/i; if (emailFormatRegex.test(email)) { return { success: true }; } return { success: false, message: "User does not have valid email format", }; })(default__users);
Validate field content length
The following code sample validates whether the value in the Name field of an account is equal to or less than 10 characters.
((account) => { const inputField = account.Name; if (inputField.length > 10) { return { success: false, message: "Account Name is too long" }; } else { return { success: true }; } })(account);
Validate if a field contains special characters
The following code sample validates whether the value in the Comments field of an invoice contains any special characters.
((invoice) => { const comments = invoice.Comments; const commentFormatRegex = /^[A-Za-z0-9 ,.]+$|^$/; if (commentFormatRegex.test(comments)) { return { success: true }; } return { success: false, message: "Invoice Comments Should Not contain special characters", }; })(invoice);
Formula function use cases
The following are common use cases of formula functions.
Calculate tax due
The following code sample updates the SalesTax__c
field with the value of multiplying the purchase amount in the PurchaseAmount__c
field by the tax rate in the TaxRate__c
field.
((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);
Calculate total production cost
The following code sample updates the Total_Production_Cost__c
field with the sum of values in the Materials_Cost__c
, Labor_Cost__c
, and Overhead_Cost__c
fields.
((default__manufacturing) => { const materialsCost = default__manufacturing.Materials_Cost__c, const laborCost = default__manufacturing.Labor_Cost__c, const overheadCost = default__manufacturing.Overhead_Cost__c, const totalProductionCost = materialsCost + laborCost + overheadCost; return { success: true, message: "The total production cost is updated", data: { Total_Production_Cost__c: totalProductionCost } }; })(default__manufacturing);
Copy a custom field value from a related object
The following code sample sets the AdditionalInfo__c
field on the Subscription Rate Plan Charge object using the value from the CustomField__c
field on the related Product Rate Plan Charge object.
((rateplancharge) => { let productRatePlanCharge = rateplancharge.ProductRatePlanCharge; let result = { success: true }; if (productRatePlanCharge.CustomField__c !== null) { result = { ...result, data: { AdditionalInfo__c: productRatePlanCharge.CustomField__c } }; } return result; })(rateplancharge);
This use case demonstrates how to access fields on joined objects related to a function's base object. For more information about object relationships, see Zuora business object model.
It is recommended to use Zuroa Workflow for use cases that require referencing objects not related to the base object or traversing multiple objects, such as multi-step operations.