Automatically validate orders before they're saved by checking business rules like maximum discount percentages. This guide walks you through creating an AI Agent that prevents orders with excessive discounts from being saved.
Table of Contents
- Overview
- Prerequisites
- Step 1: Access AI Agents
- Step 2: Create a New Agent
- Step 3: Add Conditional Logic
- Step 4: Configure Validation Responses
- Step 5: Save and Activate
- Testing Your Agent
- Troubleshooting
Overview
AI Agents allow you to automate workflows in Upsales. In this tutorial, you'll create an agent that:
- Triggers when an opportunity or order is created or updated
- Checks if the discount percentage exceeds a maximum threshold (20%)
- Blocks the save operation if the discount is too high
- Allows the save if the discount is acceptable
Use case: Ensure sales representatives cannot save orders with discounts greater than 20% without approval.
Prerequisites
Before you begin, ensure you have:
- Access to Upsales with administrator permissions
- The AI Agents feature enabled for your account
Step 1: Access AI Agents
- Log in to power.upsales.com
- In the left sidebar, click on AI Agents
You'll see the AI Agents overview page where you can manage all your agents.
Step 2: Create a New Agent
- Click Create agent in the top right corner
- Select Upsales Validation Trigger from the trigger options
- Double-click the trigger node to open its settings
- Under What, select Order from the dropdown
- Click outside the settings to close them
You'll now see your trigger node in the Agent editor.
Note: The user will wait for the agent to finish before the order saves.
Step 3: Add Conditional Logic
Now we'll add an If node to check the discount percentage.
- Click the + button next to your trigger node
- In the nodes panel, select Flow
Click on If to add it to your workflow
-
Configure the If node:
- Click on the If node to open its settings
- Set the condition as follows:
To calculate the discount percentage, we need to compare the total list price with the total sales price across all order rows. Copy and paste this expression exactly into the left value field:
| Field | Value |
|---|---|
| Left value | {{ (1 - $json.data.orderRow.map(row => row.price).sum() / $json.data.orderRow.map(row => row.listPrice).sum()) * 100 }} |
| Operator | is greater than |
| Right value | 20 |
Tip: You don't need to understand this expression — just copy and paste it exactly as shown. It calculates the discount percentage from the order rows.
- Click outside the settings to close them
The If node now has two outputs:
- true — when the discount is too high (greater than 20%)
- false — when the discount is acceptable (20% or less)
Step 4: Configure Validation Responses
You need to add a Respond to Webhook node for each output to tell Upsales whether to allow or block the save.
4a. Block the Save (when discount is too high)
- Click the + button next to the true output on the If node
- Search for "Respond to Webhook" and select it
- Configure the node:
- Set Respond With to JSON
- Enter the following in the Response Body:
{
"validationPassed": false,
"errorMessage": "Discount exceeds maximum allowed (20%)"
}
- Click outside the settings to close them
4b. Allow the Save (when discount is acceptable)
- Click the + button next to the false output on the If node
- Search for "Respond to Webhook" and select it
- Configure the node:
- Set Respond With to JSON
- Enter the following in the Response Body:
{
"validationPassed": true
}
- Click outside the settings to close them
Step 5: Save and Activate
Your complete workflow should look like this:
- Click the Save button in the top right corner
- Toggle the Inactive switch to Active to enable the agent
Important: Once activated, this agent will run for all users in your account. Make sure your validation rules are correct before enabling it.
Testing Your Agent
To test your validation agent:
Create a new order in Upsales
Add products and set a discount greater than 20%
Try to save the order
You should see an error message: "Discount exceeds maximum allowed (20%)"
Now change the discount to 20% or less
Save the order
The order should save successfully
Troubleshooting
The agent doesn't trigger
- Ensure the agent is set to Active
- Verify you selected Order under What in the trigger settings
The validation always fails or always passes
- Double-check that you copied the expression exactly as shown
- Verify the operator is set to "is greater than"
- Ensure the right value is
20(not in quotation marks)
Error messages don't appear
- Make sure
errorMessageis included in your validation failed response - Check that
validationPassedis set tofalsewithout quotation marks around the word false
JSON Response Reference
Block the save (validation failed)
{
"validationPassed": false,
"errorMessage": "Your error message here"
}
Allow the save (validation passed)
{
"validationPassed": true
}
Next Steps
- Create validation agents for other types (Subscriptions, Companies)
- Add more complex validation rules with multiple conditions
- Integrate with external systems for advanced validation logic
Need help? Contact Upsales support if you have questions about AI Agents.