Tutorial 3 - Automatically calculating a line item total
Beta feature
This feature is currently in beta, and should be considered ‘under development’. Learn about the automations beta.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
- In the web app, navigate to Settings > Configuration tools > Automations.
- Click New automation.
- In the New automation dialog, enter a Name for the automation, for example:
Tutorial 3 - Calculate line item total - Optionally, enter a Description such as:
Automatically sets Total to Quantity multiplied by UnitPrice, and clears Total when either field is empty. - Select the Record Modified template.
- 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.

-
On the workflow canvas, click the Trigger step (the blue rectangle). The step properties panel opens on the right.
-
Under the Trigger section, find the Object type field and select InvoiceLineItem from the dropdown list.
-
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.
-
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.
Tip
Run when accepts EQL (Elastic Query Language) expressions in its Advanced mode. See the EQL filter documentation for details.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.
- On the workflow canvas, click the Echo step to select it.
- 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.
- In the Steps palette on the left, locate the Update Records action. You can type
updatein the search box at the top of the palette to find it quickly. - 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

- Click the Update Records step on the workflow canvas to open its properties.
- In the Object Name field, select InvoiceLineItem.
- In the Ids field, enter the following workflow variable to reference the record that triggered the automation:
$trigger.current.UID - In the Field assignments parameter, click the + Add field link.
- In the field dropdown, select Total.
- 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.
Tip
Step input fields like Ids and field value editors support JSONata expressions: a lightweight language for querying and transforming workflow data. The* operator performs multiplication. See JSONata expressions for the full set of arithmetic and string operators available.
Tip
To browse all available workflow variables, click the $ icon next to any field, or type$ directly into the field. A variable picker will appear showing all outputs from earlier steps in the workflow.
Step 5: Activate the automation
- 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.
- If there are any configuration errors, they will be highlighted in the builder. Resolve them and click Activate again.
- 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.
-
Navigate to the InvoiceLineItem list. The default URL is
/p/InvoiceLineItem. -
Create a new record and set the following values:
Field Value Quantity 5UnitPrice 20 -
Leave the Total field empty.
-
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.

- Click the Activity tab.
- 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 | 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.
Note
Activity logs may take a few seconds to appear. If the log is empty, wait a moment and click the refresh button in the top right of the list.Verify the record field was updated
- Navigate back to the InvoiceLineItem record you created.
- Confirm that the Total field is now set to
100(5 × 20).
Test an update
To confirm the automation also fires on Update:
- Edit the record and change Quantity to
10. - Save the record.
- Return to the activity log and confirm a second workflow run completed.
- 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.Fieldin 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.UIDworkflow 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
Feedback
Was this page helpful?