diff --git a/Core ServiceNow APIs/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/README.md b/Core ServiceNow APIs/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/README.md new file mode 100644 index 0000000000..98d6e02d7b --- /dev/null +++ b/Core ServiceNow APIs/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/README.md @@ -0,0 +1,35 @@ +# 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/Core ServiceNow APIs/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_derive_dependent_fields.js b/Core ServiceNow APIs/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_derive_dependent_fields.js new file mode 100644 index 0000000000..24fa39decf --- /dev/null +++ b/Core ServiceNow APIs/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_derive_dependent_fields.js @@ -0,0 +1,45 @@ +// 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/Core ServiceNow APIs/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_validate_short_description.js b/Core ServiceNow APIs/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_validate_short_description.js new file mode 100644 index 0000000000..2fa2e64e84 --- /dev/null +++ b/Core ServiceNow APIs/GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError()/br_validate_short_description.js @@ -0,0 +1,16 @@ +// 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);