Skip to content

Commit a234d9c

Browse files
committed
Add Client Script: Mandatory Field Highlighter using proper ServiceNow methods
- Highlights empty mandatory fields with error messages - Uses g_form.showFieldMsg() and hideFieldMsg() instead of DOM manipulation - Includes onLoad script for initial highlighting - Includes onChange script for real-time updates - Follows ServiceNow best practices and official APIs
1 parent 2aa679b commit a234d9c

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Mandatory Field Highlighter
2+
3+
## Use Case / Requirement
4+
Highlight mandatory fields that are empty by showing error messages, making it easier for users to identify which required fields need to be completed.
5+
6+
## Solution
7+
Two client scripts that work together:
8+
1. **onLoad script**: Shows error messages for empty mandatory fields when form loads
9+
2. **onChange script**: Updates error messages in real-time as users fill fields
10+
3. Uses proper ServiceNow methods instead of DOM manipulation
11+
12+
## Implementation
13+
1. Create an **onLoad** client script with the code from `script.js`
14+
2. Create an **onChange** client script with the code from `onChange.js`
15+
3. Apply both scripts to the same table/form
16+
17+
## Files
18+
- `script.js`: onLoad client script for initial highlighting
19+
- `onChange.js`: onChange client script for real-time updates
20+
21+
## Notes
22+
- Uses `g_form.showFieldMsg()` and `g_form.hideFieldMsg()` methods
23+
- Follows ServiceNow best practices (no DOM manipulation)
24+
- Works with standard ServiceNow forms
25+
- Provides clear error messages for required fields
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function onChange(control, oldValue, newValue, isLoading) {
2+
if (isLoading) {
3+
return;
4+
}
5+
6+
// Update highlighting for the changed field
7+
var fieldName = control.fieldName;
8+
9+
if (g_form.isMandatory(fieldName)) {
10+
if (!newValue || newValue === '') {
11+
// Show error message for empty mandatory field
12+
g_form.showFieldMsg(fieldName, 'This field is required', 'error');
13+
} else {
14+
// Hide error message when field is filled
15+
g_form.hideFieldMsg(fieldName);
16+
}
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function onLoad() {
2+
// Highlight mandatory fields that are empty using proper ServiceNow methods
3+
highlightMandatoryFields();
4+
5+
function highlightMandatoryFields() {
6+
var allFields = g_form.getFieldNames();
7+
8+
for (var i = 0; i < allFields.length; i++) {
9+
var fieldName = allFields[i];
10+
11+
// Check if field is mandatory and visible
12+
if (g_form.isMandatory(fieldName) && g_form.isVisible(fieldName)) {
13+
var fieldValue = g_form.getValue(fieldName);
14+
15+
if (!fieldValue || fieldValue === '') {
16+
// Show warning message for empty mandatory fields
17+
g_form.showFieldMsg(fieldName, 'This field is required', 'error');
18+
} else {
19+
// Clear any existing field messages
20+
g_form.hideFieldMsg(fieldName);
21+
}
22+
}
23+
}
24+
}
25+
26+
// Store function globally so onChange scripts can call it
27+
window.updateMandatoryHighlighting = highlightMandatoryFields;
28+
}

0 commit comments

Comments
 (0)