> **Source:** https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/how-to/calculate-a-field-value > **Site:** Leegality Knowledge Base — https://knowledge.leegality.com > **About:** Leegality is a document execution platform covering eSigning, stamps, NeSL, workflows, and REST API integration. > **Navigation:** Every article on this site has a plain-text version at `.txt` (this format). To get an index of all articles with their `.txt` links, read: https://knowledge.leegality.com/llms.txt > **AI Guide:** For instructions on how to navigate this knowledge base as an AI agent, read: https://knowledge.leegality.com/ai-readable.txt --- # Calculate a field's value with a rule **Goal:** a field's value should compute itself from other fields, so nobody does the math by hand. Working example: *Stamp Group Amount of Document 1 = Loan Amount × Stamp Duty %.* This is a [Value rule](https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/rule-categories#d--value-rules) of the computed kind — multiple Subject Fields, arithmetic on the right-hand side, and a condition of "all inputs filled" rather than a threshold. ## The spec ``` TARGET field: Stamp Group Amount of Document 1 ACTION: Set Value = Loan Amount × Stamp Duty Percentage SUBJECT field: Loan Amount, Stamp Duty Percentage (pack-level custom fields) CONDITION: Both fields filled OUTCOME: Stamp Group Amount fills itself when both inputs exist; until then it is left untouched Workflow: (yours) ``` ## The rule logic This assumes both inputs are **pack-level** custom fields with API names `loanAmount` and `stampDutyPercentage` (the rate entered as a percentage, e.g. `0.5` for 0.5%), and the target is the **first document's** stamp group amount: ``` #version 2.0 var loanAmountRaw = pack.lecf_loanAmount; var rateRaw = pack.lecf_stampDutyPercentage; if(loanAmountRaw != null && (isEmpty(loanAmountRaw) == false) && rateRaw != null && (isEmpty(rateRaw) == false)) { var loanAmount = toNumber(loanAmountRaw); var rate = toNumber(rateRaw); var doc = documents[0]; if(doc != null) { doc.stamps.stampGroup.stampGroupAmount = loanAmount * rate / 100; } } ``` Two things worth noticing. The values a runner types arrive as **text**, so the logic converts them with `toNumber(...)` before doing arithmetic — every calculation rule needs this. And the whole computation sits inside the "both inputs filled" guard, so the rule stays silent until the data exists. ## Adapt these parts | Part | In the logic | Change it to | | --- | --- | --- | | Input fields | `pack.lecf_loanAmount`, `pack.lecf_stampDutyPercentage` | `lecf_` + your fields' exact API names | | The formula | `loanAmount * rate / 100` | Your arithmetic — drop `/ 100` if the rate is stored as a fraction (0.005) rather than a percentage (0.5) | | Target document | `documents[0]` | `documents[0]` is document 1, `documents[1]` is document 2, and so on | | Target field | `.stamps.stampGroup.stampGroupAmount` | Keep as is for stamp duty; other targets have other paths — see below | Writing the result somewhere else — a custom field, a template field printed on the document, a setting on an invitee — uses the same shape with a different target path. For those, send the spec to [support@leegality.com](mailto:support@leegality.com) or see Advanced: LEEQL; a state-wise duty **lookup** instead of a flat rate is also covered there. ## Configure it 1. Rule Engine panel → new rule → **Rule Title**: `Stamp duty = loan amount × rate`. 2. Paste the adapted logic into **Create Rule**. 3. **Trigger Method**: **On Change**, attached to the input fields — so the amount recalculates the moment an input changes. (Set the rule to fire on each input field; a calculation that fires on only one input goes stale when the other is edited.) 4. Save. Full configuration flow: [Build your first rule](https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/build-your-first-rule). ## Test it — and re-test on edit 1. Enter Loan Amount 10,00,000 and rate 0.5 → Stamp Group Amount becomes 5,000. 2. Edit the Loan Amount to 20,00,000 → the amount should update to 10,000. Calculations must survive *edits*, not just first entry — this is where a wrong trigger shows itself. 3. Clear one input → the previously computed value remains (the rule doesn't erase; it only writes when both inputs exist).