> **Source:** https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/how-to/auto-assign-documents > **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 --- # Auto-assign documents with a rule **Goal:** when the runner adds a document, it should be assigned to the right invitees automatically — no manual wiring, no forgotten assignments. Working example: *every newly added document is auto-assigned to all invitees.* This is an [assignment rule](https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/rule-categories#assignment-rules): it fires when something is **added** to the workflow and applies structural defaults to it. The same shape covers the mirror case — applying a default settings bundle to every newly added invitee. ## The spec ``` TARGET field: Each invitee's assigned documents ACTION: Append the newly added document to every invitee's assignments, in the invitee's own role (signer signs, reviewer reviews) SUBJECT field: The documents list (a new document was just added) CONDITION: Fires on the Add Document action OUTCOME: The moment a document is added, every invitee has it assigned; documents added earlier are unaffected Workflow: (yours) ``` ## The rule logic ``` #version 2.0 var docCount = len(documents); var lastAddedDoc = documents[docCount - 1]; if(lastAddedDoc != null) { for(invitee in invitees.inviteeCards) { var combination = invitee.inviteeCard.combinations[0]; var assignedDocs = combination.assignedDocuments; assignedDocs[len(assignedDocs)] = { "documentReference": "/documents/" + lastAddedDoc.id, "role": invitee.inviteeCard.inviteeType, "id": uuid() }; } } ``` The idiom that makes this work: the rule grabs the **last element** of the documents list (`documents[docCount - 1]`) — which, at the moment the Add Document click fires, is precisely the document that was just added. It then loops every invitee and appends an assignment that references the document and mirrors the invitee's own role, so signers get it to sign and reviewers to review. ## Adapt these parts | Part | In the logic | Change it to | | --- | --- | --- | | Who gets the document | `for(invitee in invitees.inviteeCards)` — everyone | Assigning to only *some* invitees (by role or label) adds a condition inside the loop — spec it and ask [Support](mailto:support@leegality.com), or see Advanced: LEEQL | | The role | `invitee.inviteeCard.inviteeType` — mirrors each invitee | Usually keep as is | | What triggers it | the newly added (last) document | To assign a *newly added invitee* all existing documents instead, the same last-element idiom applies to the invitee list — that mirror rule is in Advanced: LEEQL | ## Configure it 1. Rule Engine panel → new rule → **Rule Title**: `New documents auto-assign to all invitees`. 2. Paste the logic into **Create Rule**. 3. **Trigger Method**: **On Click**, attached to the **+ Add Document** control — the rule must fire at the moment of adding, because "the last document in the list" is only guaranteed to be the new one right then. This trigger attaches to a specific control, unlike the Proceed triggers. 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 1. Run the workflow with two invitees. Add a document → open each invitee's assignments: the new document appears on both, in each invitee's own role. 2. Add a second document → it too appears on both invitees; the first document's assignments are untouched. 3. Add an invitee *after* the documents (if your flow allows it) → note the new invitee does **not** get the earlier documents — that's the mirror rule, fired from Add Invitee, not this one. ## Related The category explained: [Assignment rules](https://knowledge.leegality.com/document-execution/workflows/v4/rule-engine/rule-categories#assignment-rules). Assignment rules commonly ship alongside invitee-default rules (org seal, notifications, eSign types applied to every newly added invitee) — those bundles are built in the rule language; spec what you want and send it to [support@leegality.com](mailto:support@leegality.com).