Skip to main content

Use cases and code samples

Zuora

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 LifetimeValue__c field matches the regular expression for the USD currency format.

((account) => {
    const lifetimeValue = account.LifetimeValue__c;
    const currencyFormatRegex = /^\$?\d{1,3}(,\d{3})*(\.\d{1,2})?$/;
    if (currencyFormatRegex.test(lifetimeValue)) {
        return { success: true };
    }
    return {
        success: false,
        message: "Lifetime 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);

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);