Skip to content

Commit c6759aa

Browse files
committed
removed onChange.js and simplified the script
1 parent 28e8e3d commit c6759aa

File tree

3 files changed

+35
-78
lines changed

3 files changed

+35
-78
lines changed
Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,27 @@
11
# Mandatory Field Highlighter
22

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.
3+
## Use Case
4+
Provides visual feedback for empty mandatory fields on ServiceNow forms by showing error messages when the form loads. Helps users quickly identify which required fields need to be completed.
55

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
6+
## Requirements
7+
- ServiceNow instance
8+
- Client Script execution rights
9+
- Forms with mandatory fields
1110

1211
## Implementation
13-
1. **Create onLoad script**:
14-
- New Client Script, Type: onLoad
15-
- Copy code from `script.js`
16-
- Apply to desired table
12+
1. Create a new Client Script with Type "onLoad"
13+
2. Copy the script code from script.js
14+
3. Apply to desired table/form
15+
4. Save and test on a form with mandatory fields
1716

18-
2. **Create onChange script(s)**:
19-
- New Client Script, Type: onChange
20-
- Copy code from `onChange.js`
21-
- **Important**: Replace `'FIELD_NAME'` with actual field name (e.g., 'priority')
22-
- Create separate onChange scripts for each mandatory field you want to validate
23-
- Set the "Field name" in the client script form to the specific field
24-
25-
## Files
26-
- `script.js`: onLoad client script for initial highlighting
27-
- `onChange.js`: onChange template script for real-time updates
28-
29-
## Example Usage
30-
For priority field onChange script:
31-
```javascript
32-
var fieldName = 'priority'; // Replace FIELD_NAME with 'priority'
33-
```
34-
35-
For category field onChange script:
36-
```javascript
37-
var fieldName = 'category'; // Replace FIELD_NAME with 'category'
38-
```
17+
## Features
18+
- Shows error messages under empty mandatory fields on form load
19+
- Uses proper ServiceNow client scripting APIs
20+
- No DOM manipulation or unsupported methods
21+
- Lightweight and focused functionality
3922

4023
## Notes
41-
- Uses `g_form.showFieldMsg()` and `g_form.hideFieldMsg()` methods
42-
- Follows ServiceNow best practices (no DOM manipulation)
43-
- Works with standard ServiceNow forms
44-
- Create one onChange script per mandatory field for best results
24+
- Uses g_form.showFieldMsg() method to display error messages
25+
- Only runs on form load - no real-time updates
26+
- Works with all mandatory fields automatically
27+
- Simple single-script solution

Client-Side Components/Client Scripts/Mandatory Field Highlighter/onChange.js

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
function onLoad() {
2-
// Highlight mandatory fields that are empty using proper ServiceNow methods
3-
highlightMandatoryFields();
2+
// Get all field names on the form
3+
var fieldNames = g_form.getFieldNames();
44

5-
function highlightMandatoryFields() {
6-
var allFields = g_form.getFieldNames();
5+
// Check each field
6+
for (var i = 0; i < fieldNames.length; i++) {
7+
var fieldName = fieldNames[i];
78

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-
}
9+
// Skip if field is not mandatory or not visible
10+
if (!g_form.isMandatory(fieldName) || !g_form.isVisible(fieldName)) {
11+
continue;
12+
}
13+
14+
// Get current field value
15+
var value = g_form.getValue(fieldName);
16+
17+
// Show error message if field is empty
18+
if (!value || value === '') {
19+
g_form.showFieldMsg(fieldName, 'This field is required', 'error');
2320
}
2421
}
25-
26-
// Store function globally so onChange scripts can call it
27-
window.updateMandatoryHighlighting = highlightMandatoryFields;
2822
}

0 commit comments

Comments
 (0)