Skip to content

Commit fb93964

Browse files
authored
Smart Field Validation and Dependent Field Derivation Using getError() and setError() (#2036)
* Create custom_alert.js If you need HTML beautified alert box, use this. * Create custom_alert_box.js UI Page to render the custom alert * ExampleScreenShots These are example ScreenShots for reference * Create readme.md Example Screenshots * Rename Catalog Client Script/CustomAlert/ExampleScreenShotCustomAlertInfo.png to Catalog Client Script/CustomAlert/Screenshots/ExampleScreenShotCustomAlertInfo.png * Rename Catalog Client Script/CustomAlert/ExampleScreenShotForCustomAlertSuccess.png to Catalog Client Script/CustomAlert/Screenshots/ExampleScreenShotForCustomAlertSuccess.png * Rename Catalog Client Script/CustomAlert/ExampleScrenShotForCustomAlertDanger.png to Catalog Client Script/CustomAlert/Screenshots/ExampleScrenShotForCustomAlertDanger.png * Create readme.md Notes for installation or Implementation * Create update_notes_tag_addition.js Business rule to add work notes on record if notes for tag entries are allowed. It is very hard to track who added the tag to the record and when, this will help to manage to understand who has added what tags on record. * Create update_notes_tag_removal.js Business rule to add work notes on record if notes for tag removal are allowed. It is very hard to track who removed the tag to the record and when, this will help to manage to understand who has removed what tags on record. * Create readme.md Notes for the implementation * Revert "Create update_notes_tag_addition.js" This reverts commit a56fd1f. * Revert "Create update_notes_tag_removal.js" This reverts commit deb75b1. * Revert "Create readme.md" This reverts commit 064f919. * Create br_validate_short_description.js * Create README.md * Create br_derive_dependent_fields.js * Update br_derive_dependent_fields.js * Update README.md * Delete GlideElement/Smart Field Validation and Dependent Field Derivation Using getError() and setError() directory * Create README.md Readme File * Create br_validate_short_description.js Validate Short Description BR * Create br_derive_dependent_fields.js Dependent BR
1 parent 89992b8 commit fb93964

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)