From 5b578c8409ff0f6024d8fda6a5e60215adcb2fc3 Mon Sep 17 00:00:00 2001 From: Sumith Thota <108344062+SumithThota@users.noreply.github.com> Date: Wed, 22 Oct 2025 12:53:35 +0530 Subject: [PATCH] Prevent invalid user ID --- .../Prevent Invalid User ID/README.md | 39 +++++++++++++++++++ .../Prevent Invalid User ID/script.js | 19 +++++++++ 2 files changed, 58 insertions(+) create mode 100644 Server-Side Components/Business Rules/Prevent Invalid User ID/README.md create mode 100644 Server-Side Components/Business Rules/Prevent Invalid User ID/script.js diff --git a/Server-Side Components/Business Rules/Prevent Invalid User ID/README.md b/Server-Side Components/Business Rules/Prevent Invalid User ID/README.md new file mode 100644 index 0000000000..e8d652df7f --- /dev/null +++ b/Server-Side Components/Business Rules/Prevent Invalid User ID/README.md @@ -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. + diff --git a/Server-Side Components/Business Rules/Prevent Invalid User ID/script.js b/Server-Side Components/Business Rules/Prevent Invalid User ID/script.js new file mode 100644 index 0000000000..aa8c50e910 --- /dev/null +++ b/Server-Side Components/Business Rules/Prevent Invalid User ID/script.js @@ -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); \ No newline at end of file