XRM supports a Computed field type that automatically calculates values from other fields or related records. Computed fields are read-only and evaluated on every read.
Reference other fields in the same record with arithmetic operators.
await fieldService.CreateAsync(entityId, new FieldDefinition
{
Name = "TotalPrice",
DisplayName = "Total Price",
DataType = FieldDataType.Computed,
Expression = "UnitPrice + ShippingCost"
});Count or sum child records via relationships.
await fieldService.CreateAsync(entityId, new FieldDefinition
{
Name = "OrderCount",
DisplayName = "Number of Orders",
DataType = FieldDataType.Computed,
Expression = "COUNT(Order)"
});
await fieldService.CreateAsync(entityId, new FieldDefinition
{
Name = "TotalRevenue",
DisplayName = "Total Revenue",
DataType = FieldDataType.Computed,
Expression = "SUM(OrderLine.Amount)"
});| Operator | Example |
|---|---|
+ |
FieldA + FieldB |
- |
Price - Discount |
* |
Subtotal * 1.21 |
/ |
Total / Count |
() |
(A + B) * C |
| Function | Syntax | Description |
|---|---|---|
COUNT |
COUNT(EntityName) |
Count child records linked via relationship |
SUM |
SUM(EntityName.FieldName) |
Sum a numeric field across child records |
- Field names are case-sensitive and must match exactly
- Numeric literals use dot decimal:
1.21,100,0.5 - Missing or null fields resolve to
0 - Division by zero returns
0 - Invalid expressions return no value (field stays empty)
- Evaluated on read — never stored in the database
- Always current — reflects latest data from record and children
- Skipped in validation — cannot be set by user or API
- Displayed read-only — rendered with locale formatting (Decimal format)
- Available in lists — computed values appear in record grids
- Available via API — GET responses include computed values
COUNT(EntityName) and SUM(EntityName.FieldName) work via XRM's relationship system:
- The computed field must be on the parent entity
- The
EntityNamein the expression must match the child entity'sName - A relationship must exist linking parent → child
- Only records linked via
RecordLinkare counted/summed
Expression = "Subtotal * 0.21"
Expression = "Subtotal + Tax"
(Where Tax is itself a computed field — computed fields can reference other computed fields)
Expression = "COUNT(Contact)"
Expression = "SUM(OrderLine.Amount)"
Expression = "SUM(OrderLine.UnitPrice) + SUM(OrderLine.Shipping)"
POST /api/entities/{entityId}/fields
Content-Type: application/json
{
"name": "TotalPrice",
"dataType": "Computed",
"expression": "UnitPrice + ShippingCost"
}The computed value appears in GET responses but is ignored in POST/PUT payloads.