Skip to content

Commit 242d9cb

Browse files
authored
Add Client Script: Mandatory Field Highlighter with red border (#1704)
1 parent 9973350 commit 242d9cb

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Mandatory Field Highlighter
2+
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.
5+
6+
## Requirements
7+
- ServiceNow instance
8+
- Client Script execution rights
9+
- Forms with mandatory fields
10+
11+
## Implementation
12+
1. Create a new Client Script with Type "onLoad"
13+
2. Copy the script code from script.js
14+
3. **Customize the fieldsToCheck string** with your form's mandatory field names
15+
4. Apply to desired table/form
16+
5. Save and test on a form with mandatory fields
17+
18+
## Configuration
19+
Edit the `fieldsToCheck` variable to include your form's mandatory fields as a comma-separated string:
20+
21+
```javascript
22+
// Example configurations for different forms:
23+
24+
// For Incident forms:
25+
var fieldsToCheck = 'short_description,priority,category,caller_id,assignment_group';
26+
27+
// For Request forms:
28+
var fieldsToCheck = 'short_description,requested_for,category,priority';
29+
30+
// For Change Request forms:
31+
var fieldsToCheck = 'short_description,category,priority,assignment_group,start_date,end_date';
32+
33+
// For Problem forms:
34+
var fieldsToCheck = 'short_description,category,priority,assignment_group';
35+
36+
// Custom fields (add as needed):
37+
var fieldsToCheck = 'short_description,priority,u_business_justification,u_cost_center';
38+
```
39+
40+
## Features
41+
- Shows error messages under empty mandatory fields on form load
42+
- Easy configuration with comma-separated field names
43+
- Automatically skips fields that don't exist on the form
44+
- Only processes fields that are actually mandatory and visible
45+
- Uses proper ServiceNow client scripting APIs
46+
- No DOM manipulation or unsupported methods
47+
48+
## Common Field Names
49+
- `short_description` - Short Description
50+
- `priority` - Priority
51+
- `category` - Category
52+
- `caller_id` - Caller
53+
- `requested_for` - Requested For
54+
- `assignment_group` - Assignment Group
55+
- `assigned_to` - Assigned To
56+
- `state` - State
57+
- `urgency` - Urgency
58+
- `impact` - Impact
59+
- `start_date` - Start Date
60+
- `end_date` - End Date
61+
- `due_date` - Due Date
62+
- `location` - Location
63+
- `company` - Company
64+
- `department` - Department
65+
66+
## Notes
67+
- Uses g_form.showFieldMsg() method to display error messages
68+
- Uses g_form.hasField() to safely check field existence (built into the safety checks)
69+
- Only runs on form load - provides initial validation feedback
70+
- Easy to customize for different forms by modifying the field list
71+
- Compatible with all standard ServiceNow forms
72+
- Lightweight and focused on essential functionality
73+
74+
## Example Usage
75+
For a typical incident form, simply change the configuration line to:
76+
```javascript
77+
var fieldsToCheck = 'short_description,priority,category,caller_id,assignment_group';
78+
```
79+
Save the Client Script and test on an incident form to see error messages appear under empty mandatory fields.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function onLoad() {
2+
3+
// USER CONFIGURATION: Add field names you want to check (comma-separated)
4+
var fieldsToCheck = 'short_description,priority,category,caller_id';
5+
6+
// Convert to array and process
7+
var fieldArray = fieldsToCheck.split(',');
8+
9+
// Check each field
10+
for (var i = 0; i < fieldArray.length; i++) {
11+
var fieldName = fieldArray[i];
12+
13+
// Skip if field is not mandatory or not visible
14+
if (!g_form.isMandatory(fieldName) || !g_form.isVisible(fieldName)) {
15+
continue;
16+
}
17+
18+
// Get current field value
19+
var value = g_form.getValue(fieldName);
20+
21+
// Show error message if field is empty
22+
if (!value || value === '') {
23+
g_form.showFieldMsg(fieldName, 'This field is required', 'error');
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)