1. Knowledge Base
2. How-To Guides
3. Logic, Equations, & Formulas

# How to Create Discount Codes

## In this article, you will find step-by-step instructions on how to apply discount codes using validation rules, conditional rules, and equations.

This guide uses validation rules and conditional rules to apply a percentage discount to a total when a user enters a valid code. You can work through these instructions step-by-step by adding this discount code functionality to the Bakery Manager sample app.

### Use Case

The bakery sends out discount codes in their monthly newsletters. Subscribed members can take advantage of an order discount by entering the discount code prior to checkout.

### Requirements

1. Table: The Data section of your Builder has a table that stores records that contain the information your users will use to check out. In our example, this table is named "Orders".

2. Numeric field: The "Orders" table has a numeric field to apply the discount rate to. In our example, this is a Sum field called "Order Items Total".

1. This field could be a number field, an equation field, or a formula field.

3. Edit form: An edit form is created for the orders in your Live App, allowing the user to enter a discount code.

### Steps

#### 1.  Add Discount Fields to the Orders Table

We'll start by creating the following fields in the Orders table:

• Create a Short Text field named "Discount Code".
• This will be where the user will input the discount code before checkout.
• Create a Number field named "Discount Amount".
• This field will be used to apply the discount rate to the order total. Conditional rules will be added to this field in the next step.
• Be sure to set this field up to have the result format to be decimals; decimal places of 2 and set it to a currency format.
• Create an Equation field (Numeric Equation type) named "Order Total".
• This field will be used to subtract the Discount Amount field from the Order Items Total field. In the equation editor, we'll need to enter:
`{Order Items Total} - {Discount Amount}`
• Be sure to set up this field up to have the result format to be decimals; decimal places of 2 and set it to a currency format.
• Finally, we'll need to update the equation in the pre-existing "Total Due" field to be:
`{Order Total} - {Total Charged}`

Note: The sequential order the fields are listed in is important. The "Discount Amount" field needs to be listed above the Order Total field so that it can be calculated prior to the order total.

#### 2.  Add a Validation Rule to Check for Valid Codes

The Discount Code field needs to have validation rules added in order to check for a valid code. If the entered code does not match your validation rule, they will see your custom error message.

If you would like to allow only one code per order:

• First, we'll select to edit the Discount Code field, select the "Validation Rules" tab, and then select the checkbox next to “Add validation rules”.

• Next, we'll create this validation rule:

• When “Discount Code is not LARGE-ORDER"

• Show message: “This is not a valid code, please use a different one.”

If you would like to allow multiple codes per order:

1. For the second validation criteria, we will need to create the

1. When “Discount Code is not HALF-OFF”

2. When “Discount Code is not LARGE-ORDER”

3. Show message: “This is not a valid code, please use a different one.”

#### 3.  Add Conditional Rules to Set the Discount Amount

The Discount Amount field needs conditional rules to apply a discount rate depending on which code was entered.

Each discount code can have multiple conditions. For example, if your discount also requires a total higher than \$100.

You can check for different discount codes by adding multiple conditional rules. Regardless of the number of rules, you must have one default rule that leaves this field blank if no valid code is entered.

• We'll need to select to edit the Discount Amount field, select the "Conditional Rules" tab, and then select the checkbox next to “Add conditional rules”.

• We'll create the following rules:

• Rule #1:

• When “Discount Code is HALF-OFF

• Set to a custom value: ".50

• Rule #2:

• When “Discount Code is LARGE-ORDER

• When “Order Items Total is higher than "99

• Set to a custom value: ".10"

• Rule #3:

• When: Run with every record

• Set “to a custom value: 0”

#### 4.  Calculate the Total Using an Equation

Now that the percent discounted is set, it needs to be used in conjunction with a total to calculate the order total.

We'll edit the Order Total equation field and enter the following formula in the equation editor so that it will calculate the total of the order minus the discount:

`{Line Items Total} - {Discount Amount}`

In order for this to work, the Order record must exist already, so we will need to use an edit form for this use case.

The form can include any other Order field, but it needs the Discount Code field at the minimum. This setup works well on an Order Details page where we can add that edit Order form easily.

• Under the Pages section in the Builder, and then under the "Customer > Orders Login" login page, we'll see the Order Details page. Select that page in the left bar page tree.

• Select to edit the menu view.

• Rename the menu view title to “Add Discount Code” and save the changes.

• Under the menu view, select to navigate to the newly created “Edit Order” page.

• In the page settings, update the page name to “Add Discount Code” and the Page URL to “add-discount-code”.

• Select the checkbox next to “Display page in a modal popup” and select “Save Settings”.

• Back in the page preview, select to add an edit form to update Order records.

• Remove the view title of “Edit Order” and select “Save”.

• Rename the edit form view title to “Add Discount Code”.

• Remove all inputs on the form except for the Discount Code field.

• Under the edit form's settings section, rename the “Submit Button Text” to “Apply Discount Code”.

• Finally, under the Form Rules section, update the default submit rule submit action to “Redirect to an existing page” and select the page “My Orders > Order Details”. This will ensure the page will refresh and correctly apply the Discount Code.

• Be sure to select "Save" after the submit rule edit.

#### 6.  Update the Checkout to Use the Order Total Field

In order to make sure the checkout uses this new Order Total field that will include the discount amount, the “Checkout” Payment form needs to have the Payment Total field updated from the Order Items Total field to the Order Total field.

• Select on the "Checkout" page in the left bar page tree. Then, select to edit the “Checkout” Payment view.

• Under the Settings section, change the field selected in the drop-down for "Payment total field" from “Order Items Total” to “Order Total”. Be sure to select Save Changes on the Payment view after this edit.

• You’ll likely want to make the same edit from the above step on the Payment Summary section fields.

And with that, you have successfully configured a workflow using conditional rules, validation rules, and equations to create discount codes! 🚀