> For the complete documentation index, see [llms.txt](https://docs.rulecube.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rulecube.com/how-to-guides/ruleset-development/entities/create-an-entity.md).

# Create an Entity

## Procedure

Let's create a compound entity because if you master that, you'll also know how to create elementary entities. You'll create the compound `Invoice` entity as shown above, including the calculation of `Amount` at the `InvoiceItem` level and the `TotalAmount` at the Invoice level.

For compound entities, it's necessary to work bottom-up because parent entities require child entities to be available. So, first, create `ProductItem`, then `InvoiceItem`, and finally, `Invoice`:

### Step 1: Create the `ProductItem` entity

1. Create a ruleset named `TotalInvoiceAmount`.
2. Create an entity named `entProductItem`.&#x20;
3. Click **Add property**.\
   ↳ The **Input/Property** page opens.
4. In The **Name** field, enter `ProductID`. And in the **Type** list, select `string`.
5. Save the property.
6. Repeat steps 3 to 5 to create the `Description` property as a `string` and the `Price` property as a `number`.\
   ↳ Finally, your entity should look like this:<br>

   <div align="left"><figure><img src="/files/HBPwPO38YifxmJPvpbsi" alt=""><figcaption></figcaption></figure></div>

### Step 2: Create the `InvoiceItem` entity

1. Create an entity named `entInvoiceItem`.&#x20;
2. Add a property named `Count` (**Type**=`number`).
3. Add a property named Product. And in the **Type** list, select `entProductItem`.\
   ↳ Now, `entInvoiceItem` and `entProductItem` are connected, and `entInvoiceItem` has become a compound entity. Rulecube shows this with a little cube icon in front of the `entInvoiceItem` entity:<br>

   <div align="left"><figure><img src="/files/E7djAts6Mh7jRUa6YzFc" alt=""><figcaption></figcaption></figure></div>
4. Add a calculated property for `Amount` (**Type**=`number`) and set its **Expression** to:<br>

   ```javascript
   Count * Product.Price
   ```

   \
   **Explanation**\
   Because `entInvoiceItem` and `entProductItem` are connected, you can use the `Product.Price` to calculate the amount for invoice items.<br>
5. Save the entity.\
   ↳ The content of `entInvoiceItem` should look like this:<br>

   <div align="left"><figure><img src="/files/mbq60WfdwQWe3fMNNoV5" alt=""><figcaption></figcaption></figure></div>

   Notice that Rulecube places the<img src="/files/cCMXM75T93XxN4Wd5CqV" alt="" data-size="line"> of *input* in front of regular properties and the ![](/files/LxkpHh3R7gTAst1x4oA1) of *rule* in front of calculated properties.

### Step 3: Create the `Invoice` entity

1. Create an entity named `entInvoice`.&#x20;
2. Add a property named `InvoiceItems` and set its **Type** to `array` with **Sub type** = `entInvoiceItem`.\
   ↳ By defining this property as an array, an invoice can have multiple items. Furthermore, `entInvoice` and `entInvoiceItem` are connected now. And because `entInvoiceItem` is connected to `entProductItem`, `entInvoice` is also connected to `entProductItem`.&#x20;
3. Add a calculated property for `TotalAmount` (**Type**=`number`) and set its **Expression** to:<br>

   ```javascript
   InvoiceItems.sum(item => item.Amount)
   ```

   \
   **Explanation**\
   This is an [arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) that summarizes all the item amounts into the `TotalAmount`.<br>
4. Save the entity.\
   ↳ Your compound entity structure now should look like this:<br>

   <div align="left"><figure><img src="/files/2bLOo504wpi9nKL6u7Io" alt=""><figcaption></figcaption></figure></div>

### Step 4: Test your ruleset

[Try out the ruleset](/how-to-guides/ruleset-development/try-out-a-ruleset.md) and enter input like this:

```json
{
  "Invoice": {
    "InvoiceItems": [
      {
        "Product": {
          "ProductID": "PK-207-U",
          "Description": "Rubber bands",
          "Price": 11.75
        },
        "Count": 8
      },
      {
        "Product": {
          "ProductID": "KD-883-J",
          "Description": "6mm tube - 1mtr",
          "Price": 5.15
        },
        "Count": 5
      }      
    ]
  }
}
```

After executing, you should get a result like this:

```json
{
  "InvoiceItems": [
    {
      "Product": {
        "ProductID": "PK-207-U",
        "Description": "Rubber bands",
        "Price": 11.75
      },
      "Count": 8,
      "Amount": 94
    },
    {
      "Product": {
        "ProductID": "KD-883-J",
        "Description": "6mm tube - 1mtr",
        "Price": 5.15
      },
      "Count": 5,
      "Amount": 25.75
    }
  ],
  "TotalAmount": 119.75
}
```

With the amount calculated per item and the total amount calculated for the invoice.

## Additional resources

If you have a JSON schema or an XSD for your entity structure, Rulecube has a much easier and quicker way to create entities as described in the [Drag and Drop](/how-to-guides/ruleset-development/entities/drag-and-drop-a-json-schema-or-xsd-to-create-entities.md) guide.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.rulecube.com/how-to-guides/ruleset-development/entities/create-an-entity.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
