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.
01 | ((default__vehicle => { |
02 | const address = default__vehicle.Address__c; |
03 | if ([ '30324' , '30326' , '30328' , '94702' , '94027' ].includes(address)){ |
04 | return { success: true }; |
08 | message: "Sorry, we do not deliver to this location" |
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.
02 | const dueDate = invoice.Due_Date__c; |
03 | const currentDate = new Date(); |
04 | const daysUntilDue = Math.floor((dueDate - currentDate) / (1000 * 60 * 60 * 24)); |
05 | if (daysUntilDue <= 90) { |
06 | return { success: true }; |
10 | message: "Invoice is overdue and past the 90 days threshold." |
Validate currency format
The following code sample validates whether the value in the LifetimeValue__c
field matches the regular expression for the USD currency format.
02 | const lifetimeValue = account.LifetimeValue__c; |
03 | const currencyFormatRegex = /^\$?\d{1,3}(,\d{3})*(\.\d{1,2})?$/; |
04 | if (currencyFormatRegex.test(lifetimeValue)) { |
05 | return { success: true }; |
09 | message: "Lifetime Value should be a valid USD currency format (e.g., $100,456.03)." |
Validate URL protocol
The following code sample validates whether the value in the Source__c
field is a valid URL.
01 | ((default__vehicle) => { |
02 | const urlValue = default__vehicle.Source__c; |
03 | const url = new URL(urlValue); |
04 | if (url.protocol === 'http:' || url.protocol === 'https:' ) { |
05 | return { success: true }; |
09 | message: "Source should have a valid URL format (e.g., http or https.)" |
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
.
2 | if (account.Country__c === 'United States' && !account.BillingState__c) { |
5 | message: "Billing State is required for accounts in the United States." |
8 | return { success: true }; |
Validate social security number format
The following code sample validates whether the value in the SSN__c
field is a valid social security number.
01 | ((default__Credit_Check) => { |
02 | const ssnValue = default__Credit_Check.SSN__c; |
03 | const ssnFormatRegex = /^(\d{3}-\d{2}-\d{4}|\d{9})$/; |
04 | if (ssnFormatRegex.test(ssnValue)) { |
05 | return { success: true }; |
09 | message: "The SSN should have a valid format (###-##-####)." |
11 | })(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.
02 | const creditLimit = account.Credit_Limit__c; |
03 | if (creditLimit >= 0) { |
04 | return { success: true }; |
08 | message: "Credit limit cannot be negative." |
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.
02 | const lifetimeValue = default__users.Email__c; |
03 | const currencyFormatRegex = /^([A-Z0-9_+-]+\.?)*[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/i; |
04 | if (currencyFormatRegex.test(lifetimeValue)) { |
05 | return { success: true }; |
10 | "User does not have valid email format" , |
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.
2 | const inputField = account.Name; |
3 | if (inputField.length > 10) { |
4 | return { success: false , message: "Account Name is too long" }; |
7 | return { success: true }; |
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.
02 | const lifetimeValue = invoice.Comments; |
03 | const currencyFormatRegex = /^[A-Za-z0-9 ,.]+$|^$/; |
04 | if (currencyFormatRegex.test(lifetimeValue)) { |
05 | return { success: true }; |
10 | "Invoice Comments Should Not contain special characters" , |
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.
01 | ((default__vehicle) => { |
02 | const purchaseAmount = default__vehicle.PurchaseAmount__c; |
03 | const taxRate = default__vehicle.TaxRate__c; |
04 | const salesTax = purchaseAmount * taxRate; |
07 | message: "The sales tax is updated." , |
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.
01 | ((default__manufacturing) => { |
02 | const materialsCost = default__manufacturing.Materials_Cost__c, |
03 | const laborCost = default__manufacturing.Labor_Cost__c, |
04 | const overheadCost = default__manufacturing.Overhead_Cost__c, |
05 | const totalProductionCost = materialsCost + laborCost + overheadCost; |
08 | message: "The total production cost is updated" , |
10 | Total_Production_Cost__c: totalProductionCost |
13 | })(default__manufacturing); |