|
1 | | -A business rule that verifies all checklist items are completed before allowing the record to progress to the next status. |
| 1 | +# ✅ Prevent State Change with Incomplete Checklist (Business Rule) |
2 | 2 |
|
3 | | -The business rule consists of three main parts: |
| 3 | +## 📁 Location |
| 4 | +**Category:** `Server-Side Components` |
| 5 | +**Subcategory:** `Business Rules` |
| 6 | +**Snippet Folder:** `Checklist Completion Validator` |
4 | 7 |
|
5 | | -Part A: Looks up all checklists (checklist table) tied to the current record (document = current.sys_id). |
| 8 | +--- |
6 | 9 |
|
7 | | -Part B: For each checklist, query the checklist_item records: |
| 10 | +## 📌 Description |
8 | 11 |
|
9 | | - Only items in that checklist. |
| 12 | +This **Business Rule** enforces completion of all associated **checklist items** on an **incident** before its **state can be changed to "In Progress"**. |
10 | 13 |
|
11 | | - Only items that are not complete (complete = false). |
12 | | - |
13 | | -Part C: If any incomplete items exist, an error message is displayed and the action is aborted. |
| 14 | +If any checklist item is still incomplete, the rule **prevents the state transition**, displays a clear error message, and **aborts the update**. This ensures that required tasks or validations tied to the incident are completed before agents begin working on it. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 🚀 Features |
| 19 | + |
| 20 | +- ✅ Enforces checklist completion before progressing the incident state |
| 21 | +- ✅ Prevents workflow bypassing by validating in the **Before Update** stage |
| 22 | +- ✅ Uses `GlideRecord` to check both checklist and checklist_item records |
| 23 | +- ✅ Displays user-friendly error messages in the UI |
| 24 | +- ✅ Supports structured processes and improves agent accountability |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## 📄 Script: `incompleteChkListShowErrMsg.js` |
| 29 | + |
| 30 | +```javascript |
| 31 | +// Business Rule: Before Update on Incident table |
| 32 | +// Condition: current.state == 2 (In Progress) |
| 33 | + |
| 34 | +(function executeRule(current, previous /*null when async*/) { |
| 35 | + var checklistGR = new GlideRecord("checklist"); |
| 36 | + checklistGR.addQuery("document", current.sys_id); |
| 37 | + checklistGR.query(); |
| 38 | + |
| 39 | + // Only run if state is changing to 'In Progress' |
| 40 | + if (current.state == 2 && previous.state != 2) { |
| 41 | + |
| 42 | + while (checklistGR.next()) { |
| 43 | + // Query checklist items tied to this record that are not complete |
| 44 | + var itemGR = new GlideRecord("checklist_item"); |
| 45 | + itemGR.addQuery("checklist", checklistGR.sys_id); |
| 46 | + itemGR.addQuery("document", current.sys_id); // Matches the current record |
| 47 | + itemGR.addQuery("complete", false); // Only incomplete items |
| 48 | + itemGR.query(); |
| 49 | + |
| 50 | + // If any incomplete item exists, abort the action |
| 51 | + if (itemGR.hasNext()) { |
| 52 | + gs.addErrorMessage("This record has incomplete checklist items. Please complete them before proceeding."); |
| 53 | + current.setAbortAction(true); |
| 54 | + break; // Stop after first checklist with incomplete items |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +})(current, previous); |
| 59 | + |
| 60 | +🛠️ How to Use |
| 61 | + |
| 62 | +1) Create a new Business Rule on the incident table. |
| 63 | +2) Set the following properties: |
| 64 | + When to run: Before |
| 65 | + Update: ✅ Checked |
| 66 | + Condition: current.state == 2 && previous.state != 2 |
| 67 | +3) Paste the provided script into the Script field. |
| 68 | +4) Ensure checklist functionality is enabled and used in your incident form. |
| 69 | + |
| 70 | +📸 Example Scenario |
| 71 | + |
| 72 | +1) An agent attempts to set an incident’s state to “In Progress”. |
| 73 | +2) The record has an attached checklist with at least one unchecked item. |
| 74 | +3) The system displays the message: |
| 75 | +❌ "This record has incomplete checklist items. Please complete them before proceeding." |
| 76 | +4) The state change is blocked until all checklist items are marked complete. |
| 77 | + |
| 78 | +📂 File Structure |
| 79 | + |
| 80 | +Server-Side Components/ |
| 81 | +└── Business Rules/ |
| 82 | + └── Checklist Completion Validator/ |
| 83 | + ├── README.md |
| 84 | + └── incompleteChkListShowErrMsg.js |
| 85 | + |
| 86 | +⚙️ Requirements |
| 87 | + |
| 88 | +1) Checklist plugin must be enabled (com.glide.ui.checklist) |
| 89 | +2) Applicable only when incidents use the checklist feature |
| 90 | +3) Tag/state ID for “In Progress” is typically 2 (verify if customized) |
| 91 | + |
| 92 | +✅ Contribution Checklist Compliance |
| 93 | + |
| 94 | +✔️ Folder structure follows repository requirements |
| 95 | +✔️ Script is scoped and relevant to ServiceNow development |
| 96 | +✔️ Contains a complete and descriptive README.md |
| 97 | +✔️ No XML exports or system-specific data |
| 98 | +✔️ Clean use of native APIs (GlideRecord, setAbortAction, addErrorMessage) |
| 99 | + |
| 100 | +👨💻 Author |
| 101 | + |
| 102 | +Contributor: @Shweyy123 |
| 103 | +Pull Request: Pending |
| 104 | +Script Name: incompleteChkListShowErrMsg.js |
| 105 | + |
| 106 | +📘 License |
| 107 | + |
| 108 | +This script is provided for educational and internal use. Always validate logic in a development or sub-production instance before deploying to production. |
| 109 | + |
| 110 | +🧩 Optional Enhancements |
| 111 | + |
| 112 | +1) Show which checklist items are incomplete in the error message |
| 113 | +2) Automatically focus on the checklist section of the form |
| 114 | +3) Extend to other tables like change_request or problem |
0 commit comments