Skip to content

Commit 04c7e4c

Browse files
authored
Improve Mandatory check script and Readme (#1984)
* Update script.js - Adjusted the script to instead of not displaying the state field, flash the fields that are not filled in. This makes more sense and is using this existing function: https://www.servicenow.com/docs/bundle/zurich-api-reference/page/script/useful-scripts/reference/r_UsefulFieldScripts.html#d471717e187 - The getMissingFields() returns an array. Joining it with commas to improve readability of the info message. - To prevent further execution added return statement. * Update README.md Updated the readme text. - Highlighting what the code provides. - Putting focus on the additional functionality of the error message that is provided by the function already, which was not mentioned in the original. Updated with 2 new screenshots * Update script.js
1 parent ea59f67 commit 04c7e4c

File tree

2 files changed

+23
-19
lines changed
  • Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()

2 files changed

+23
-19
lines changed
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
**Client Script**
1+
# Mandatory Field Check on Form Change
22

3-
Client script which is showing how to use g_form.mandatoryCheck() function, which allows to easily detect if any of mandatory field is not filled on record.
3+
This client script demonstrates how to use `g_form.mandatoryCheck()` to validate whether all mandatory fields on a form are filled.
44

5-
**Example configuration**
5+
It is typically used in an **onChange** catalog client script to trigger validation when a specific field changes.
66

7-
![Coniguration](ScreenShot_1.PNG)
7+
If mandatory fields are missing, the script:
8+
- Displays an info message listing the missing fields. Note: the red message is the existing functionality that will give you an error message.
9+
- Visually highlights each missing field using a flashing effect to guide the user.
810

9-
**Example execution**
11+
This approach improves user experience by clearly indicating which fields require attention.
1012

11-
![Execution](ScreenShot_2.PNG)
13+
**Example configuration**
14+
<img width="1122" height="638" alt="image" src="https://github.com/user-attachments/assets/31f86ae7-27fe-4921-8d8b-391eaa55304d" />
15+
16+
**Example execution**
17+
<img width="1029" height="495" alt="image" src="https://github.com/user-attachments/assets/b8384507-4292-41f4-9155-9be10195493e" />
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
2-
if (isLoading || newValue === '') {
3-
return;
4-
}
5-
6-
//Client script, which shows how to use mandatoryCheck() function
7-
//mandatoryCheck() allows validating if all mandatory fields are filled
8-
//If all mandatory fields are filled it return true, otherwise it returns false
2+
if (isLoading || newValue === '') {
3+
return;
4+
}
95

10-
//Check if all mandatory fields are filled on record
6+
// Check if all mandatory fields are filled
117
if (!g_form.mandatoryCheck()) {
12-
13-
//Example action when not all mandatory fields are filled - display message and remove state field
8+
//if not get all missing fields
149
var missing = g_form.getMissingFields();
15-
g_form.addInfoMessage("State field removed, because not all mandatory fields are filled: " + missing);
16-
g_form.setDisplay('state', false);
10+
//Info message displaying the fields that are missing
11+
g_form.addInfoMessage("Please complete the following mandatory fields: " + missing.join(", "));
12+
//go through missing fields and flash them
13+
missing.forEach(function (fieldName) {
14+
g_form.flash(fieldName, "#FFFACD",0); // Flash to draw attention
15+
});
1716
}
18-
1917
}

0 commit comments

Comments
 (0)