diff --git a/Server-Side Components/Business Rules/Validate Checklist items/README.md b/Server-Side Components/Business Rules/Validate Checklist items/README.md index ff4afbb271..2f32741f34 100644 --- a/Server-Side Components/Business Rules/Validate Checklist items/README.md +++ b/Server-Side Components/Business Rules/Validate Checklist items/README.md @@ -1,13 +1,114 @@ -A business rule that verifies all checklist items are completed before allowing the record to progress to the next status. +# βœ… Prevent State Change with Incomplete Checklist (Business Rule) -The business rule consists of three main parts: +## πŸ“ Location +**Category:** `Server-Side Components` +**Subcategory:** `Business Rules` +**Snippet Folder:** `Checklist Completion Validator` -Part A: Looks up all checklists (checklist table) tied to the current record (document = current.sys_id). +--- -Part B: For each checklist, query the checklist_item records: +## πŸ“Œ Description - Only items in that checklist. +This **Business Rule** enforces completion of all associated **checklist items** on an **incident** before its **state can be changed to "In Progress"**. - Only items that are not complete (complete = false). - -Part C: If any incomplete items exist, an error message is displayed and the action is aborted. +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. + +--- + +## πŸš€ Features + +- βœ… Enforces checklist completion before progressing the incident state +- βœ… Prevents workflow bypassing by validating in the **Before Update** stage +- βœ… Uses `GlideRecord` to check both checklist and checklist_item records +- βœ… Displays user-friendly error messages in the UI +- βœ… Supports structured processes and improves agent accountability + +--- + +## πŸ“„ Script: `incompleteChkListShowErrMsg.js` + +```javascript +// Business Rule: Before Update on Incident table +// Condition: current.state == 2 (In Progress) + +(function executeRule(current, previous /*null when async*/) { + var checklistGR = new GlideRecord("checklist"); + checklistGR.addQuery("document", current.sys_id); + checklistGR.query(); + + // Only run if state is changing to 'In Progress' + if (current.state == 2 && previous.state != 2) { + + while (checklistGR.next()) { + // Query checklist items tied to this record that are not complete + var itemGR = new GlideRecord("checklist_item"); + itemGR.addQuery("checklist", checklistGR.sys_id); + itemGR.addQuery("document", current.sys_id); // Matches the current record + itemGR.addQuery("complete", false); // Only incomplete items + itemGR.query(); + + // If any incomplete item exists, abort the action + if (itemGR.hasNext()) { + gs.addErrorMessage("This record has incomplete checklist items. Please complete them before proceeding."); + current.setAbortAction(true); + break; // Stop after first checklist with incomplete items + } + } + } +})(current, previous); + +πŸ› οΈ How to Use + +1) Create a new Business Rule on the incident table. +2) Set the following properties: + When to run: Before + Update: βœ… Checked + Condition: current.state == 2 && previous.state != 2 +3) Paste the provided script into the Script field. +4) Ensure checklist functionality is enabled and used in your incident form. + +πŸ“Έ Example Scenario + +1) An agent attempts to set an incident’s state to β€œIn Progress”. +2) The record has an attached checklist with at least one unchecked item. +3) The system displays the message: +❌ "This record has incomplete checklist items. Please complete them before proceeding." +4) The state change is blocked until all checklist items are marked complete. + +πŸ“‚ File Structure + +Server-Side Components/ +└── Business Rules/ + └── Checklist Completion Validator/ + β”œβ”€β”€ README.md + └── incompleteChkListShowErrMsg.js + +βš™οΈ Requirements + +1) Checklist plugin must be enabled (com.glide.ui.checklist) +2) Applicable only when incidents use the checklist feature +3) Tag/state ID for β€œIn Progress” is typically 2 (verify if customized) + +βœ… Contribution Checklist Compliance + +βœ”οΈ Folder structure follows repository requirements +βœ”οΈ Script is scoped and relevant to ServiceNow development +βœ”οΈ Contains a complete and descriptive README.md +βœ”οΈ No XML exports or system-specific data +βœ”οΈ Clean use of native APIs (GlideRecord, setAbortAction, addErrorMessage) + +πŸ‘¨β€πŸ’» Author + +Contributor: @Shweyy123 +Pull Request: Pending +Script Name: incompleteChkListShowErrMsg.js + +πŸ“˜ License + +This script is provided for educational and internal use. Always validate logic in a development or sub-production instance before deploying to production. + +🧩 Optional Enhancements + +1) Show which checklist items are incomplete in the error message +2) Automatically focus on the checklist section of the form +3) Extend to other tables like change_request or problem diff --git a/Server-Side Components/Business Rules/Validate Checklist items/incompleteChkListShowErrMsg.js b/Server-Side Components/Business Rules/Validate Checklist items/incompleteChkListShowErrMsg.js index f05bc4dc92..a111d68eba 100644 --- a/Server-Side Components/Business Rules/Validate Checklist items/incompleteChkListShowErrMsg.js +++ b/Server-Side Components/Business Rules/Validate Checklist items/incompleteChkListShowErrMsg.js @@ -1,19 +1,21 @@ -//Business Rule: before update on the incident table -//Condition: changing state to 'In Progress' +// Business Rule: Before Update on Incident table +// Condition: current.state == 2 (In Progress) + (function executeRule(current, previous /*null when async*/) { - var checklistGR = new GlideRecord("checklist"); - checklistGR.addQuery("document", current.sys_id); - checklistGR.query(); + // Only run if state is changing to 'In Progress' + if (current.state == 2 && previous.state != 2) { - while (checklistGR.next()) { + // Query checklist items tied to this record that are not complete var itemGR = new GlideRecord("checklist_item"); - itemGR.addQuery("checklist", checklistGR.sys_id); - itemGR.addQuery("complete", false); + itemGR.addQuery("document", current.sys_id); // Matches the current record + itemGR.addQuery("complete", false); // Only incomplete items itemGR.query(); + // If any incomplete item exists, abort the action if (itemGR.hasNext()) { - gs.addErrorMessage('Checklist has incomplete items. Please complete all before assigning it back.'); + gs.addErrorMessage("This record has incomplete checklist items. Please complete them before proceeding."); current.setAbortAction(true); } } })(current, previous); +