diff --git a/Client-Side Components/Client Scripts/Mandatory Field Highlighter/README.md b/Client-Side Components/Client Scripts/Mandatory Field Highlighter/README.md new file mode 100644 index 0000000000..6ba73500c3 --- /dev/null +++ b/Client-Side Components/Client Scripts/Mandatory Field Highlighter/README.md @@ -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 \ No newline at end of file diff --git a/Client-Side Components/Client Scripts/Mandatory Field Highlighter/onChange.js b/Client-Side Components/Client Scripts/Mandatory Field Highlighter/onChange.js new file mode 100644 index 0000000000..cc631c3011 --- /dev/null +++ b/Client-Side Components/Client Scripts/Mandatory Field Highlighter/onChange.js @@ -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); + } + } +} \ No newline at end of file diff --git a/Client-Side Components/Client Scripts/Mandatory Field Highlighter/script.js b/Client-Side Components/Client Scripts/Mandatory Field Highlighter/script.js new file mode 100644 index 0000000000..98889fa14a --- /dev/null +++ b/Client-Side Components/Client Scripts/Mandatory Field Highlighter/script.js @@ -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; +} \ No newline at end of file