> **Source:** https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/build-your-first-rule > **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 --- # Build your first rule In this tutorial you'll configure one complete, working rule from start to finish: > **IF** Loan Amount is greater than ₹10,00,000, **THEN** the Reviewer's Name becomes **Mandatory**. It's the classic Mandatory rule: big-ticket loans get a human check, small ones flow straight through. A rule's logic is written in Leegality's **rule language**. You won't write any of it today — the logic for this rule is provided below, ready to paste — so you can focus on the configuration flow: title, logic, trigger method, test. What every line of the language means is a topic for Advanced: LEEQL, once the concepts have settled. ## Before you start You'll need: - **A v4 workflow open in the editor**, with a **reviewer as the first invitee**. The provided rule logic checks the first invitee card, so the order matters here. If you're practising, duplicate an existing workflow rather than editing a live one. - **A Loan Amount custom field added at the pack level.** The rule watches this field, so it must exist first. If it doesn't, [create the custom field](https://knowledge.leegality.com/document-execution/workflows/v4/custom-fields/create-a-custom-field) with the API name `loanAmount`, then [add it at the pack level](https://knowledge.leegality.com/document-execution/workflows/v4/custom-fields/add-fields-to-workflow) — pack level suits a value shared across the whole pack. Come back when it appears in the editor. - **The spec.** Even for a tutorial, start the way you always should — with the six lines written down: ``` TARGET field: Reviewer (1st invitee) · Name field ACTION: Set Mandatory = true SUBJECT field: Loan Amount CONDITION: Loan Amount > ₹10,00,000 OUTCOME: Reviewer's Name is Mandatory above ₹10L; Optional at or below ₹10L Workflow: (your practice workflow) ``` ## Step 1 — Open the Rule Engine panel In the workflow editor, open the **Rule Engine** from the **left panel** — it sits alongside Custom Fields and Visualisation, and is available at every stage of the workflow. Choose the option to create a new rule. ## Step 2 — Name the rule Give the rule a **Rule Title** that states what it does, not just what it touches. A title you'll thank yourself for later: ``` Reviewer mandatory above ₹10L loan ``` > **Tip — Naming habit** > > Workflows accumulate rules. ` ` as a title pattern keeps the rule list scannable — compare the title above with something like "Loan rule 2". ## Step 3 — Add the rule logic **Create Rule** is where the rule's logic lives, written in the rule language. Paste this in: ``` #version 2.0 var loanAmountRaw = pack.lecf_loanAmount; if(loanAmountRaw != null && (isEmpty(loanAmountRaw) == false)) { var loanAmountNumber = toNumber(loanAmountRaw); if(loanAmountNumber > 1000000) { var card = invitees.inviteeCards[0]; if(card != null) { var reviewerName = card.inviteeCard.inviteeDetails[0].inviteeName; if(reviewerName == null || isEmpty(reviewerName)) { throw "Reviewer Name is mandatory for loans above Rs. 10,00,000"; } } } } ``` One adjustment before pasting: `lecf_loanAmount` refers to your Loan Amount custom field by its **API name** with the `lecf_` prefix. If you named the field something other than `loanAmount` when creating it, change that part to match. You don't need to read the language yet — but notice that every line of your spec is in there: | Spec line | Where it lives in the logic | | --- | --- | | SUBJECT field | `pack.lecf_loanAmount` — the pack-level Loan Amount | | CONDITION | `loanAmountNumber > 1000000` — greater than ₹10,00,000 | | TARGET + ACTION | the check on the first invitee's Name, enforced with an error | | OUTCOME, both sides | the error fires only above the threshold; at or below it, nothing blocks | That mapping is the whole relationship between the [six-line spec](https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/turn-a-requirement-into-a-rule) and the language: the spec is the intake form, the language is the rule. > **Note — Why an error message instead of a mandatory flag?** > > This is how "make X mandatory" rules typically ship: the rule **validates when the user clicks Proceed** and blocks the stage with a message naming exactly what's missing. The effect is the same — you can't continue without the reviewer — and the runner is told precisely why. ## Step 4 — Choose the Trigger Method The **Trigger Method** decides *when* the workflow evaluates your rule. The dropdown offers seven options: | Trigger Method | When the rule runs | | --- | --- | | On Click | When the user clicks the field. | | On Change | When the field's value changes. | | On Blur | When the user moves focus away from the field (clicks or tabs out of it). | | On Proceed Invitee | When the user clicks Proceed to leave the Invite stage. | | On Proceed Create | When the user clicks Proceed to leave the Create stage. | | On Proceed Finalise | When the user clicks Proceed in the Finalise stage. | | On Proceed Template | When the user clicks Proceed to leave the Template stage. | For this rule, choose **On Proceed Invitee**: it runs at the moment the user clicks **Proceed** to leave the Invite stage — exactly when all invitee details should be complete. Why not On Change? A validation like this one belongs at Proceed: it checks the finished state, once, at the moment it matters. On Change fires while data is still being typed — right for rules that should react live (a value recalculating, a field appearing), wrong for a completeness check. ## Step 5 — Set where the rule applies **Field Execution On** sets where the rule's outcome is applied. Save the rule. ## Step 6 — Test it A rule isn't done until you've watched it fire — in both directions: 1. Run the workflow. Enter a Loan Amount of **15,00,000**, leave the reviewer's Name empty, and click **Proceed** on the Invite stage. The workflow should block you with the message: *Reviewer Name is mandatory for loans above Rs. 10,00,000*. 2. Fill in the reviewer's Name and click Proceed again — it should go through. 3. Now change the Loan Amount to **5,00,000**, clear the reviewer's Name, and Proceed — it should go through without any error. Testing both sides matters: step 3 catches the classic mistake where a rule enforces its condition but was written to fire in all cases. > **Note — Rule didn't fire?** > > The three most common causes: the **Trigger Method** doesn't match how you're testing (a Proceed-triggered rule only runs when you click Proceed); the **field name** in the logic doesn't match your custom field's API name (`lecf_` + exact API name); or the **reviewer isn't the first invitee**, so the rule is checking a different card. ## What you built — and what transfers Every rule follows today's motions: open the panel → title → rule logic → trigger method → test both directions. Only the logic changes — and you don't have to write it from scratch: - **Adapt a pattern.** The how-to guides for each category — [mandatory](https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/how-to/make-a-field-mandatory), visibility, editable, [value](https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/how-to/calculate-a-field-value) — provide rule logic to copy and adjust, the way you adjusted `lecf_loanAmount` today. - **Learn the language.** Advanced: LEEQL explains what you pasted, line by line, and how to write your own. - **Or don't.** Write the [six-line spec](https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/turn-a-requirement-into-a-rule) and send it to [support@leegality.com](mailto:support@leegality.com) — Leegality writes the rule for you.