From 2ec188d318995daec3b7e9f08f26168a4e158e84 Mon Sep 17 00:00:00 2001 From: Ravi Chandra <149189748+ravichandra1998g@users.noreply.github.com> Date: Sat, 11 Oct 2025 22:04:20 +0530 Subject: [PATCH] =?UTF-8?q?Revert=20"Smart=20Field=20Validation=20and=20De?= =?UTF-8?q?pendent=20Field=20Derivation=20Using=20getError(=E2=80=A6"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit fb9396433f9f50792300f1e59dab6fe9ad284358. --- .../README.md | 35 --------------- .../br_derive_dependent_fields.js | 45 ------------------- .../br_validate_short_description.js | 16 ------- 3 files changed, 96 deletions(-) delete mode 100644 GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/README.md delete mode 100644 GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_derive_dependent_fields.js delete mode 100644 GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_validate_short_description.js diff --git a/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/README.md b/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/README.md deleted file mode 100644 index 98d6e02d7b..0000000000 --- a/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# Smart Field Validation and Dependent Field Derivation Using GlideElement.getError() - -This project demonstrates how to use `GlideElement.setError()` and `GlideElement.getError()` -to perform validation in one Business Rule and field derivation in another, without repeating logic. - -## 📘 Overview - -This snippet demonstrates how to share validation state and error messages between multiple Business Rules using `GlideElement.setError()` and `GlideElement.getError()` in ServiceNow. - -By propagating validation context across Business Rules, developers can: - -- Avoid repeated validation logic. -- Trigger dependent field updates only when a field passes validation. -- Maintain consistent and clean data flow between sequential rules. - -This technique is especially useful when different validation or derivation rules are split by purpose or owned by different teams. - ---- - -## 🧠 Concept - -When one Business Rule sets an error on a field using `setError()`, the error message persists in memory for that record during the same transaction. -A later Business Rule (executing at a higher order) can then retrieve that message using `getError()` and make data-driven decisions. - -### Flow: -1. BR #1 (`Validate Short Description`) checks text length. -2. BR #2 (`Derive Dependent Fields`) runs only if no validation error exists. -3. Category, Subcategory, and Impact are derived dynamically. - -## 🚀 Benefits - -- ✅ Reduces redundant validation checks -- ✅ Improves rule execution efficiency -- ✅ Keeps logic modular and maintainable -- ✅ Provides better visibility and control in field validations diff --git a/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_derive_dependent_fields.js b/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_derive_dependent_fields.js deleted file mode 100644 index 24fa39decf..0000000000 --- a/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_derive_dependent_fields.js +++ /dev/null @@ -1,45 +0,0 @@ -// Name: Derive Dependent Fields -// Table: Incident -// When: before insert or before update -// Order: 200 - -(function executeRule(current, previous /*null when async*/) { - - // Only proceed if short_description changed or new record - if (!(current.operation() === 'insert' || current.short_description.changes())) { - return; - } - - var errorMsg = current.short_description.getError(); - - if (errorMsg) { - gs.info('[BR:200 - Derive] Skipping field derivation due to prior error → ' + errorMsg); - return; - } - - // Proceed only if no prior validation error - var desc = current.getValue('short_description').toLowerCase(); - - // Example 1: Derive category - if (desc.includes('server')) { - current.category = 'infrastructure'; - current.subcategory = 'server issue'; - } else if (desc.includes('email')) { - current.category = 'communication'; - current.subcategory = 'email problem'; - } else if (desc.includes('login')) { - current.category = 'access'; - current.subcategory = 'authentication'; - } else { - current.category = 'inquiry'; - current.subcategory = 'general'; - } - - // Example 2: Derive impact - if (desc.includes('critical') || desc.includes('outage')) { - current.impact = 1; // High - } else { - current.impact = 3; // Low - } - -})(current, previous); diff --git a/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_validate_short_description.js b/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_validate_short_description.js deleted file mode 100644 index 2fa2e64e84..0000000000 --- a/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_validate_short_description.js +++ /dev/null @@ -1,16 +0,0 @@ -// Name: Validate Short Description -// Table: Incident -// When: before insert or before update -// Order: 100 - -(function executeRule(current, previous /*null when async*/) { - var short_desc = current.getValue('short_description'); - - // Validate only for new records or when field changes - if (current.operation() === 'insert' || current.short_description.changes()) { - if (!short_desc || short_desc.trim().length < 40) { - current.short_description.setError('Short description must be at least 40 characters long.'); - current.setAbortAction(true); - } - } -})(current, previous);