Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

# Prevent Invalid User ID

## Overview
This **ServiceNow Business Rule** prevents inserting or updating a record when:
- `user_name` is missing or invalid.
- Both `first_name` and `last_name` are missing or invalid.

## Functionality Breakdown

### 1. `isInvalid(value)`
- Detects invalid values in user fields.
- Returns `true` if:
- Value is `null`, `undefined`, or empty (`""`)
- Value (after trimming spaces and lowering case) equals `"null"`

Example:
```javascript
isInvalid(null); // true
isInvalid(""); // true
isInvalid("NULL"); // true
isInvalid("john"); // false
```

### 2. `current.setAbortAction(true)`
- Stops the record from being inserted or updated.
- Used inside **Before Business Rules**.
- Prevents saving invalid data to the database.

### 3. `gs.addErrorMessage("...")`
- Displays a user-friendly error message at the top of the form.
- Helps users understand *why* the save was blocked.


## Notes
- Case-insensitive — handles "null", "NULL", "Null", etc.
- Works best in **Before Business Rules** to stop invalid data before saving.
- Adding `gs.addErrorMessage()` helps users understand the validation reason.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(function executeRule(current, previous) {
// Utility function to validate if a field is null, empty, or contains "null"/"NULL"
function isInvalid(value) {
return !value || value.toString().trim().toLowerCase() === "null";
}

// Abort action if the user_name field is invalid
if (isInvalid(current.user_name)) {
gs.addErrorMessage("User name is invalid");
current.setAbortAction(true);
}

// Abort action if both first_name and last_name are invalid
if (isInvalid(current.first_name) && isInvalid(current.last_name)) {
gs.addErrorMessage("Either first name or last name must be provided");
current.setAbortAction(true);
}

})(current, previous);
Loading