Skip to content

Commit d29ced4

Browse files
authored
Merge pull request #2041 from mandeepkaran/mandeepkaran-patch-GE2
Smart Field Validation and Dependent Field Derivation Using getError() and setError()
2 parents 9c65ed9 + 6e6b3fe commit d29ced4

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Smart Field Validation and Dependent Field Derivation Using GlideElement.getError()
2+
3+
This project demonstrates how to use `GlideElement.setError()` and `GlideElement.getError()`
4+
to perform validation in one Business Rule and field derivation in another, without repeating logic.
5+
6+
## 📘 Overview
7+
8+
This snippet demonstrates how to share validation state and error messages between multiple Business Rules using `GlideElement.setError()` and `GlideElement.getError()` in ServiceNow.
9+
10+
By propagating validation context across Business Rules, developers can:
11+
12+
- Avoid repeated validation logic.
13+
- Trigger dependent field updates only when a field passes validation.
14+
- Maintain consistent and clean data flow between sequential rules.
15+
16+
This technique is especially useful when different validation or derivation rules are split by purpose or owned by different teams.
17+
18+
---
19+
20+
## 🧠 Concept
21+
22+
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.
23+
A later Business Rule (executing at a higher order) can then retrieve that message using `getError()` and make data-driven decisions.
24+
25+
### Flow:
26+
1. BR #1 (`Validate Short Description`) checks text length.
27+
2. BR #2 (`Derive Dependent Fields`) runs only if no validation error exists.
28+
3. Category, Subcategory, and Impact are derived dynamically.
29+
30+
## 🚀 Benefits
31+
32+
- ✅ Reduces redundant validation checks
33+
- ✅ Improves rule execution efficiency
34+
- ✅ Keeps logic modular and maintainable
35+
- ✅ Provides better visibility and control in field validations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Name: Derive Dependent Fields
2+
// Table: Incident
3+
// When: before insert or before update
4+
// Order: 200
5+
6+
(function executeRule(current, previous /*null when async*/) {
7+
8+
// Only proceed if short_description changed or new record
9+
if (!(current.operation() === 'insert' || current.short_description.changes())) {
10+
return;
11+
}
12+
13+
var errorMsg = current.short_description.getError();
14+
15+
if (errorMsg) {
16+
gs.info('[BR:200 - Derive] Skipping field derivation due to prior error → ' + errorMsg);
17+
return;
18+
}
19+
20+
// Proceed only if no prior validation error
21+
var desc = current.getValue('short_description').toLowerCase();
22+
23+
// Example 1: Derive category
24+
if (desc.includes('server')) {
25+
current.category = 'infrastructure';
26+
current.subcategory = 'server issue';
27+
} else if (desc.includes('email')) {
28+
current.category = 'communication';
29+
current.subcategory = 'email problem';
30+
} else if (desc.includes('login')) {
31+
current.category = 'access';
32+
current.subcategory = 'authentication';
33+
} else {
34+
current.category = 'inquiry';
35+
current.subcategory = 'general';
36+
}
37+
38+
// Example 2: Derive impact
39+
if (desc.includes('critical') || desc.includes('outage')) {
40+
current.impact = 1; // High
41+
} else {
42+
current.impact = 3; // Low
43+
}
44+
45+
})(current, previous);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Name: Validate Short Description
2+
// Table: Incident
3+
// When: before insert or before update
4+
// Order: 100
5+
6+
(function executeRule(current, previous /*null when async*/) {
7+
var short_desc = current.getValue('short_description');
8+
9+
// Validate only for new records or when field changes
10+
if (current.operation() === 'insert' || current.short_description.changes()) {
11+
if (!short_desc || short_desc.trim().length < 40) {
12+
current.short_description.setError('Short description must be at least 40 characters long.');
13+
current.setAbortAction(true);
14+
}
15+
}
16+
})(current, previous);

0 commit comments

Comments
 (0)