Tutorial 3 - Automatically calculating a line item total

Learn how to create an automation that keeps a Total field in sync with Quantity and Unit Price on a custom record.

Overview

In this tutorial you will create an automation that automatically sets the Total field on an invoice line item to the correct value whenever Quantity or Unit Price is set or changed. This is a practical example of the calculated field pattern: keeping a derived value in sync with its source fields without any manual effort.

By the end of this tutorial you will have:

  • Created a new automation
  • Configured a record modified trigger that fires on both Insert and Update operations
  • Written an EQL filter that limits the trigger to changes on the source fields
  • Added and configured an Update Records action with a JSONata arithmetic expression
  • Saved and activated the automation
  • Tested the automation by creating and then editing an invoice line item record
  • Verified the Total field was calculated correctly

Prerequisites

  • You must have the Administrator role, or a role that includes the “Modify webhooks and triggered actions” permission.
  • You should be familiar with the automation builder UI. If not, read Get to know the automation builder UI first.
  • Completing Tutorial 1 and Tutorial 2 first is recommended.
  • The InvoiceLineItem custom object must exist and have the following fields configured before you start:
Field type Field label Field name Description
Integer Quantity Quantity Number of units
Decimal Unit Price UnitPrice Price per unit
Decimal Total Total Calculated total (Quantity × Unit Price)

If this object does not yet exist, create it before proceeding. See Custom objects for an overview, or follow your organization’s process for adding new objects and fields.


Step 1: Create a new automation

  1. In the web app, navigate to Settings > Configuration tools > Automations.
  2. Click New automation.
  3. In the New automation dialog, enter a Name for the automation, for example:
    Tutorial 3 - Calculate line item total
    
  4. Optionally, enter a Description such as:
    Automatically sets Total to Quantity multiplied by UnitPrice, and clears Total when either field is empty.
    
  5. Select the Record Modified template.
  6. Click OK to continue to the automation builder.

The automation builder opens with a default trigger step and is pre-populated with an Echo action by default.


Step 2: Configure the trigger

The trigger will fire whenever an InvoiceLineItem record is created or updated, but only when Quantity or UnitPrice actually changed. This avoids running the calculation on every unrelated edit to the record.

The trigger step properties panel

  1. On the workflow canvas, click the Trigger step (the blue rectangle). The step properties panel opens on the right.

  2. Under the Trigger section, find the Object type field and select InvoiceLineItem from the dropdown list.

  3. Under Operations, enable both the Insert and Update checkboxes.

    Enabling both operations means the automation runs when a line item is first created and again whenever the record is updated later.

  4. Under Run when, click Advanced and enter the following EQL expression:

    Current.Quantity != Previous.Quantity OR Current.UnitPrice != Previous.UnitPrice
    

On an Insert operation, Previous values are null: so if either field has a value, the filter is satisfied and the automation runs. On an Update, it only fires when one of the two source fields actually changed.


Step 3: Replace the Echo action with an Update Records action

The template pre-populates an Echo action as a placeholder. For this automation you need an Update Records action that writes the calculated total back to the record.

  1. On the workflow canvas, click the Echo step to select it.
  2. Press the Delete key on your keyboard, the trash icon in the top right corner of the step, or click the Delete step button in the step properties panel, to delete it.
  3. In the Steps palette on the left, locate the Update Records action. You can type update in the search box at the top of the palette to find it quickly.
  4. Drag the Update Records step from the palette onto the workflow canvas and drop it onto the placeholder below the trigger step.

Step 4: Configure the Update Records action

The Update Records action properties panel

  1. Click the Update Records step on the workflow canvas to open its properties.
  2. In the Object Name field, select InvoiceLineItem.
  3. In the Ids field, enter the following workflow variable to reference the record that triggered the automation:
    $trigger.current.UID
    
  4. In the Field assignments parameter, click the + Add field link.
  5. In the field dropdown, select Total.
  6. In the value editor, enter the following expression:
    $trigger.current.Quantity != null and $trigger.current.UnitPrice != null ? $trigger.current.Quantity * $trigger.current.UnitPrice : null
    

This is a conditional (ternary) expression. If both Quantity and UnitPrice have values, it multiplies them and writes the result to Total. If either field is empty, it sets Total to null: clearing the field rather than leaving a stale or incorrect value.


Step 5: Activate the automation

  1. In the automation builder header, click Activate. This automation is currently a Draft: clicking Activate validates the whole workflow and makes it live in one step. If you’d rather leave it as a draft for now, click Save as Draft instead, and activate it later from the automation detail page.
  2. If there are any configuration errors, they will be highlighted in the builder. Resolve them and click Activate again.
  3. Once saved successfully, click the Close button to leave the automation builder. You will be shown the automation detail page for the automation you just created, now showing a status of Active.

The automation will fire the next time an InvoiceLineItem record is created or updated.


Step 6: Create an invoice line item to trigger the automation

Now you will create an InvoiceLineItem record to test that the automation fires and sets the Total field correctly.

  1. Navigate to the InvoiceLineItem list. The default URL is /p/InvoiceLineItem.

  2. Create a new record and set the following values:

    Field Value
    Quantity 5
    UnitPrice 20
  3. Leave the Total field empty.

  4. Save the record.

Creating the record fires an Insert operation on the InvoiceLineItem object. Because both Quantity and UnitPrice are set, the automation runs and calculates the total.


Step 7: Verify the Total was calculated

First, confirm the automation ran by checking the activity log, then verify the record itself was updated.

Check the activity log

After saving the record, navigate to Settings > Configuration tools > Automations and click the name of the Tutorial 3 - Calculate line item total entry.

The automation activity tab

  1. Click the Activity tab.
  2. Each row represents one workflow execution. Click the most recent (topmost) row to expand it and see the individual step messages for that run.

You should see entries similar to:

Timestamp Event type Status Message
{date/time} Trigger Info Automation workflow started
{date/time} Workflow Info Started action ‘update-records’: Update 1 record of type InvoiceLineItem
{date/time} Workflow Info Completed action ‘update-records’: Update 1 record of type InvoiceLineItem
{date/time} Workflow Info Automation workflow completed successfully

Note the entries within an expanded execution are ordered oldest to newest, showing the order the steps actually ran in.

Verify the record field was updated

  1. Navigate back to the InvoiceLineItem record you created.
  2. Confirm that the Total field is now set to 100 (5 × 20).

Test an update

To confirm the automation also fires on Update:

  1. Edit the record and change Quantity to 10.
  2. Save the record.
  3. Return to the activity log and confirm a second workflow run completed.
  4. Open the record again and confirm Total is now 200 (10 × 20).

You should be able to see from the log and the record that:

  • The trigger detected the new or changed line item
  • The workflow ran the Update Records step
  • The Total field was set to the product of Quantity and UnitPrice
  • The workflow completed successfully

What you learned

In this tutorial you:

  • Configured a trigger with both Insert and Update operations to handle new records and edits in a single automation
  • Used Current.Field != Previous.Field in an EQL filter to limit the automation to changes that affect the calculated result
  • Used a JSONata conditional expression to calculate a value from two fields, and clear the result field when either input is missing
  • Used the $trigger.current.UID workflow variable to target the record that caused the trigger
  • Tested the automation for both the Insert and Update cases

Suggested improvements

Round to a fixed number of decimal places

If UnitPrice can have more than two decimal places, the product may produce a result with more precision than you want to store. Wrap the multiplication in JSONata’s $round() function to control this:

$trigger.current.Quantity != null and $trigger.current.UnitPrice != null ? $round($trigger.current.Quantity * $trigger.current.UnitPrice, 2) : null

The second argument to $round() sets the number of decimal places.

Next steps

  • Experiment with other arithmetic operators in the value expression, for example, apply a discount: $trigger.current.Quantity * $trigger.current.UnitPrice * 0.9
  • Proceed to the next tutorial