## Question Logic

Question logic lets you build an adaptive interview experience where clients only see questions that are relevant to their situation. Rather than presenting every possible question to every client, you define conditions — based on earlier answers — that determine whether a question or a whole page should appear. This keeps your questionnaire focused, reduces completion time, and prevents clients from being confused by questions that don’t apply to them. Gavel supports logic at two levels: **question-level logic** (show or hide a single question) and **page-level logic** (show or hide an entire page of questions). Both work the same way and can include multiple `AND`/`OR` conditions.

---

## Question-level logic

Question logic controls the visibility of an individual question based on how the client answered a prior question.

1. **Open the question editor**  
   In your workflow builder, navigate to the question you want to make conditional. Click the **Edit Logic** button on that question.

2. **Add a condition**  
   In the logic panel, choose whether the question should be **shown** or **hidden** when the condition is met. Then select the variable (from a prior question) and the answer value you want to trigger that behavior.

3. **Add multi-condition logic (optional)**  
   Click **Add Conditions** to add another rule. You can chain multiple conditions using **AND** (all conditions must be true) or **OR** (any condition must be true) to handle more complex scenarios.

4. **"X" Out of the Logic Menu and Save Workflow**  
   Click on the “X” on the upper right hand side of the Logic Menu. Then be sure to “Save” your workflow. During the interview, Gavel evaluates the logic in real time — the question appears or disappears based on what the client has already answered.

### Example scenarios

- **Show a spouse-related question only if the client is married**  
   Set the condition on the “Spouse’s Name” question to **Show if** `MaritalStatus` equals "Married". Clients who select any other marital status skip the question entirely.

- **Hide an explanation question after a qualifying answer**  
   Set the condition on a follow-up explanation field to **Hide if** `HasPriorConviction` equals `False`. Only clients who answered “Yes” to the prior conviction question will see the explanation prompt.

- **Show a question based on multiple conditions**  
   Use **AND** logic: show the “Trust Beneficiary Name” question only if `DocumentType` equals "Trust" **AND** `ClientHasBeneficiaries` is `True`. Both conditions must be satisfied for the question to appear.

---

## Page-level logic

Page logic works the same way as question logic, but controls the visibility of an entire questionnaire page. If the conditions are not met, the client skips the page completely — none of the questions on that page are asked, and their variables remain blank.

1. **Locate the page**  
   In the workflow builder, find the page you want to make conditional.

2. **Open the logic panel**  
   Click the **Logic** icon on the page (it looks like a logic tree or branching diagram).

3. **Add conditions**  
   Choose **Show if** or **Hide if**, then select the variable and answer value. Add additional conditions with **Add Conditions** using **AND** or **OR** as needed.

4. **"X" Out of the Logic Menu and Save Workflow**  
   Click on the “X” on the upper right hand side of the Logic Menu. Then be sure to “Save” your workflow. Gavel will route clients past this page entirely when the conditions are not satisfied.

Page-level logic is especially useful when you have several questions that all depend on the same prior answer. Rather than adding logic to each question individually, you can group them on a single page and apply one condition to the whole page.

---

## Tips for building reliable logic

- Always place the question that supplies the condition _before_ the question that depends on it in the questionnaire. Logic can only reference answers from prior questions — not later ones.
- If a question is hidden by logic, its variable will be blank in the generated document. Make sure any conditional content in your document template accounts for blank values using `{% if VariableName %}` guards.
- You can combine question logic and page logic in the same workflow. A common pattern is to use page logic to route clients into a specialized section, then use question logic within that section to fine-tune which fields appear.
