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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Mandatory Field Highlighter

## Use Case / Requirement
Highlight mandatory fields that are empty by showing error messages, making it easier for users to identify which required fields need to be completed.

## Solution
Two client scripts that work together:
1. **onLoad script**: Shows error messages for empty mandatory fields when form loads
2. **onChange script**: Updates error messages in real-time as users fill fields
3. Uses proper ServiceNow methods instead of DOM manipulation

## Implementation
1. Create an **onLoad** client script with the code from `script.js`
2. Create an **onChange** client script with the code from `onChange.js`
3. Apply both scripts to the same table/form

## Files
- `script.js`: onLoad client script for initial highlighting
- `onChange.js`: onChange client script for real-time updates

## Notes
- Uses `g_form.showFieldMsg()` and `g_form.hideFieldMsg()` methods
- Follows ServiceNow best practices (no DOM manipulation)
- Works with standard ServiceNow forms
- Provides clear error messages for required fields
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}

// Update highlighting for the changed field
var fieldName = control.fieldName;

if (g_form.isMandatory(fieldName)) {
if (!newValue || newValue === '') {
// Show error message for empty mandatory field
g_form.showFieldMsg(fieldName, 'This field is required', 'error');
} else {
// Hide error message when field is filled
g_form.hideFieldMsg(fieldName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function onLoad() {
// Highlight mandatory fields that are empty using proper ServiceNow methods
highlightMandatoryFields();

function highlightMandatoryFields() {
var allFields = g_form.getFieldNames();

for (var i = 0; i < allFields.length; i++) {
var fieldName = allFields[i];

// Check if field is mandatory and visible
if (g_form.isMandatory(fieldName) && g_form.isVisible(fieldName)) {
var fieldValue = g_form.getValue(fieldName);

if (!fieldValue || fieldValue === '') {
// Show warning message for empty mandatory fields
g_form.showFieldMsg(fieldName, 'This field is required', 'error');
} else {
// Clear any existing field messages
g_form.hideFieldMsg(fieldName);
}
}
}
}

// Store function globally so onChange scripts can call it
window.updateMandatoryHighlighting = highlightMandatoryFields;
}
Loading