Skip to content

Commit 7907898

Browse files
committed
added fields configuration by user instead of g_form.getFields method
1 parent c6759aa commit 7907898

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

Client-Side Components/Client Scripts/Mandatory Field Highlighter/README.md

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,69 @@ Provides visual feedback for empty mandatory fields on ServiceNow forms by showi
1111
## Implementation
1212
1. Create a new Client Script with Type "onLoad"
1313
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
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+
```
1639

1740
## Features
1841
- 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
1945
- Uses proper ServiceNow client scripting APIs
2046
- No DOM manipulation or unsupported methods
21-
- Lightweight and focused functionality
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
2265

2366
## Notes
2467
- 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
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.

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
function onLoad() {
2-
// Get all field names on the form
3-
var fieldNames = g_form.getFieldNames();
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(',');
48

59
// Check each field
6-
for (var i = 0; i < fieldNames.length; i++) {
7-
var fieldName = fieldNames[i];
10+
for (var i = 0; i < fieldArray.length; i++) {
11+
var fieldName = fieldArray[i];
812

913
// Skip if field is not mandatory or not visible
1014
if (!g_form.isMandatory(fieldName) || !g_form.isVisible(fieldName)) {

0 commit comments

Comments
 (0)