Skip to content

Commit 5b578c8

Browse files
committed
Prevent invalid user ID
1 parent 5770383 commit 5b578c8

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
# Prevent Invalid User ID
3+
4+
## Overview
5+
This **ServiceNow Business Rule** prevents inserting or updating a record when:
6+
- `user_name` is missing or invalid.
7+
- Both `first_name` and `last_name` are missing or invalid.
8+
9+
## Functionality Breakdown
10+
11+
### 1. `isInvalid(value)`
12+
- Detects invalid values in user fields.
13+
- Returns `true` if:
14+
- Value is `null`, `undefined`, or empty (`""`)
15+
- Value (after trimming spaces and lowering case) equals `"null"`
16+
17+
Example:
18+
```javascript
19+
isInvalid(null); // true
20+
isInvalid(""); // true
21+
isInvalid("NULL"); // true
22+
isInvalid("john"); // false
23+
```
24+
25+
### 2. `current.setAbortAction(true)`
26+
- Stops the record from being inserted or updated.
27+
- Used inside **Before Business Rules**.
28+
- Prevents saving invalid data to the database.
29+
30+
### 3. `gs.addErrorMessage("...")`
31+
- Displays a user-friendly error message at the top of the form.
32+
- Helps users understand *why* the save was blocked.
33+
34+
35+
## Notes
36+
- Case-insensitive — handles "null", "NULL", "Null", etc.
37+
- Works best in **Before Business Rules** to stop invalid data before saving.
38+
- Adding `gs.addErrorMessage()` helps users understand the validation reason.
39+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(function executeRule(current, previous) {
2+
// Utility function to validate if a field is null, empty, or contains "null"/"NULL"
3+
function isInvalid(value) {
4+
return !value || value.toString().trim().toLowerCase() === "null";
5+
}
6+
7+
// Abort action if the user_name field is invalid
8+
if (isInvalid(current.user_name)) {
9+
gs.addErrorMessage("User name is invalid");
10+
current.setAbortAction(true);
11+
}
12+
13+
// Abort action if both first_name and last_name are invalid
14+
if (isInvalid(current.first_name) && isInvalid(current.last_name)) {
15+
gs.addErrorMessage("Either first name or last name must be provided");
16+
current.setAbortAction(true);
17+
}
18+
19+
})(current, previous);

0 commit comments

Comments
 (0)