---
title: Rules engine
slug: rules-engine
abstract: The OrderCloud platform now includes a rules engine, initially powering order approval workflows
content_type: concept
audience: \[developer, architect]
---
# Rules engine

The OrderCloud platform now includes a rules engine, initially powering order approval workflows. This system executes externally defined rules to extend platform behavior, similar to how extended properties (`XP`) extend the data model.

## Rules engine concepts

### Rule definition

A rule consists of:

- If-then statement
- External logic definition
- Custom expressions
- Defined actions

### Platform integration

OrderCloud provides:

- Action definitions
- Execution environment
- Expression evaluation
- Rule management

## Implementation example

### Approval workflow

Create an order approval rule:

1. Create manager `UserGroup`
2. Create `ApprovalRule`
3. Set `ApprovingGroupID`
4. Define expression:

```javascript
order.Total > 200 and order.xp.MyCustomProperty = 'XYZ'
```

### Expression components

Available properties:

- `order`: Matches Order model
- Includes extended properties

Supported operators:

- Comparison: `=`, `<`, `<=`, `>=`, `<>`
- Logical: `and`, `or`, `not`
- Mathematical: `+`, `-`, `*`, `/`

Value formatting:

- Strings: Single quotes
- Dates: `#6/15/2016#`
- Parentheses: Group expressions

## Line item operations

### Collection functions

The rules engine provides collection functions for line item evaluation:

### Cost center example

```javascript
// All items over $200 in cost center
order.Total > 200 and items.any(CostCenter = 'ABC')

// Specific items over $200
items.total(CostCenter = 'ABC') > 200

// Complex conditions
items.quantity(ProductID = 'P1' or ProductID = 'P2') > 5
```

### Available functions

- `items.any`: Any item matches filter
- `items.all`: All items match filter
- `items.quantity()`: Sum of matching quantities
- `items.count()`: Number of matching items
- `items.total`: Dollar amount comparison

## Multi-level approvals

### Rule chaining

Check other rule approvals:

```javascript
order.approved('id_of_some_other_rule')
```

### Complex approval chains

```javascript
(order.Total > 20 and order.approved('rule_id_1')) or
(not item.any(ProductID = 'QQQ') and approved('rule_id_2'))
```

## Implementation guidelines

### Best practices

1. Keep rules simple
2. Split complex rules
3. Test thoroughly
4. Document clearly

### Debugging tips

1. Start simple
2. Add complexity gradually
3. Test each condition
4. Verify assumptions

## Future applications

### Potential use cases

- Custom validation
- Time-based approvals
- Dynamic pricing
- Coupon eligibility
- Inventory management
- Webhook triggers

### Platform strategy

The rules engine supports our [Flexibility over Features](https://doc.sitecore.com/ordercloud/en/developers/platform/flexibility-over-features.html) approach by:

- Enabling customization
- Supporting business logic
- Extending functionality
- Maintaining flexibility

## Related reading

- [Approval rules](https://doc.sitecore.com/ordercloud/en/developers/platform/approval-rules.html)
- [Catalog visibility](https://doc.sitecore.com/ordercloud/en/developers/platform/catalog-visibility.html)
- [Promotions](https://doc.sitecore.com/ordercloud/en/developers/platform/promotions.html)

