# Add Calculation Logic

ONEWEB Smart Forms support **custom calculation logic** to dynamically compute values based on user input, business rules, or runtime data.

There are **two approaches** for implementing calculation logic in Smart Forms:

1. **JavaScript Calculation** – client‑side, lightweight calculations
2. **Manual Java Class** – server‑side calculations executed via AJAX

Each approach is suited to different complexity and performance requirements.

***

### JavaScript Calculation (Client‑Side)

**JavaScript-based calculation logic** is typically used for **simple and immediate calculations**, such as:

* Calculating totals or subtotals
* Deriving values based on field input
* Real‑time UI updates without server calls

JavaScript logic is executed on the client side, providing quick feedback to users.

To attach JavaScript calculation logic, ONEWEB uses **Entity Actions**, similar to JavaScript validation.

> Refer to **Entity Actions** documentation for details on attaching JavaScript files to an entity.

***

#### Attach JavaScript via Entity Action

1. Add an **Entity Action** and specify the JavaScript file name.
2. Implement the calculation logic inside the JavaScript file.
3. Bind the calculation function to form fields or events as required.

This approach is recommended for **simple calculations** that do not require server‑side data or complex business logic.

***

### Manual Java Class Calculation (Server‑Side)

For **complex calculations**, ONEWEB provides the ability to execute a **Manual Java class** through **AJAX calls**.

This approach is suitable when calculations:

* Require server‑side data
* Must follow strict business rules
* Depend on database queries or external systems
* Should not be exposed to the client side

The page communicates asynchronously with the Manual Java class to retrieve calculated results.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2Firf5CqtnMuazs1Itfjzi%2F0?alt=media" alt=""><figcaption></figcaption></figure>

***

#### Implement Manual Java Calculation Class

A Manual Java class used for calculation must:

* Implement the **`ManualInterface`**
* Override the **`processManual()`** method

This method contains the calculation logic and returns the computed result to the Smart Form.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FyHaRIaXneKFbrGRZWszz%2F1?alt=media" alt=""><figcaption></figcaption></figure>

Inside `processManual()`, developers can:

* Read input parameters from the request
* Perform calculations
* Query data sources if required
* Return results to the client through AJAX

***

### Choosing the Right Approach

| Approach          | Use Case                                   |
| ----------------- | ------------------------------------------ |
| JavaScript        | Simple, UI‑level calculations              |
| Manual Java Class | Complex, server‑side business calculations |

In many applications, both approaches can be combined—using JavaScript for instant feedback and Manual Java classes for authoritative business logic.

***

### Summary

ONEWEB Smart Forms provide flexible mechanisms for implementing calculation logic:

* **JavaScript calculations** for fast, client‑side computation
* **Manual Java class calculations** for complex, secure, server‑side processing
* Seamless integration with Entity Actions and AJAX
* Support for enterprise‑grade business logic

This dual‑layer calculation model ensures both **performance** and **business rule integrity** while delivering a responsive user experience.


---

# Agent Instructions: 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:

```
GET https://docs.onewebstack.com/building-apps/ui-page-designer/ux-ui-customization/smart-form-customization/add-calculation-logic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
