Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e5d45a7
Create custom_alert.js
mandeepkaran Oct 5, 2024
12f7b57
Create custom_alert_box.js
mandeepkaran Oct 5, 2024
74fcc33
ExampleScreenShots
mandeepkaran Oct 5, 2024
8a3b974
Create readme.md
mandeepkaran Oct 5, 2024
7990eea
Rename Catalog Client Script/CustomAlert/ExampleScreenShotCustomAlert…
mandeepkaran Oct 5, 2024
f50a5b5
Rename Catalog Client Script/CustomAlert/ExampleScreenShotForCustomAl…
mandeepkaran Oct 5, 2024
01659da
Rename Catalog Client Script/CustomAlert/ExampleScrenShotForCustomAle…
mandeepkaran Oct 5, 2024
da8f30e
Create readme.md
mandeepkaran Oct 5, 2024
a56fd1f
Create update_notes_tag_addition.js
mandeepkaran Oct 6, 2024
deb75b1
Create update_notes_tag_removal.js
mandeepkaran Oct 6, 2024
064f919
Create readme.md
mandeepkaran Oct 6, 2024
58758ea
Revert "Create update_notes_tag_addition.js"
mandeepkaran Oct 6, 2024
3cfa425
Revert "Create update_notes_tag_removal.js"
mandeepkaran Oct 6, 2024
e80fe0f
Revert "Create readme.md"
mandeepkaran Oct 6, 2024
a5e2eb7
Merge branch 'ServiceNowDevProgram:main' into main
mandeepkaran Oct 6, 2024
39f6fca
Create br_validate_short_description.js
mandeepkaran Oct 11, 2025
a004c9c
Create README.md
mandeepkaran Oct 11, 2025
2c802d0
Create br_derive_dependent_fields.js
mandeepkaran Oct 11, 2025
b8607ce
Update br_derive_dependent_fields.js
mandeepkaran Oct 11, 2025
228d67b
Update README.md
mandeepkaran Oct 11, 2025
cab3fa0
Delete GlideElement/Smart Field Validation and Dependent Field Deriva…
mandeepkaran Oct 11, 2025
8995645
Create README.md
mandeepkaran Oct 11, 2025
56f02e6
Create br_validate_short_description.js
mandeepkaran Oct 11, 2025
0dec817
Create br_derive_dependent_fields.js
mandeepkaran Oct 11, 2025
63d2b02
Merge branch 'main' into mandeepkaran-patch-GE1
mandeepkaran Oct 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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);
Loading