Skip to Content

Multi-field Dependent Validation

Validate combinations of multiple fields together, such as ensuring end date is after start date or validating business logic across fields.

onSubmit Table: change_request
#onSubmit #validation #multi-field #business-logic #date-validation #complex-validation

Script Code

JavaScript
1function onSubmit() {
2  // Configuration: Define validation rules
3  var validationErrors = [];
4
5  // Validation 1: Date range validation
6  var startDate = g_form.getValue('start_date');
7  var endDate = g_form.getValue('end_date');
8
9  if (startDate && endDate) {
10    var start = new Date(startDate);
11    var end = new Date(endDate);
12
13    if (end < start) {
14      validationErrors.push('End date must be after start date');
15      g_form.showFieldMsg('end_date', 'Must be after start date', 'error');
16    }
17
18    // Check minimum duration (e.g., at least 1 hour)
19    var durationMs = end - start;
20    var durationHours = durationMs / (1000 * 60 * 60);
21
22    if (durationHours < 1) {
23      validationErrors.push('Change window must be at least 1 hour');
24      g_form.showFieldMsg('end_date', 'Minimum 1 hour duration required', 'error');
25    }
26  }
27
28  // Validation 2: Risk and impact correlation
29  var risk = g_form.getValue('risk');
30  var impact = g_form.getValue('impact');
31  var priority = g_form.getValue('priority');
32
33  // High risk must have high/medium impact
34  if (risk === '1' && (impact === '3' || impact === '4')) {  // High risk, Low impact
35    validationErrors.push('High risk changes must have High or Medium impact');
36    g_form.showFieldMsg('impact', 'High risk requires higher impact rating', 'error');
37  }
38
39  // Critical priority requires high risk or high impact
40  if (priority === '1' && risk !== '1' && impact !== '1') {
41    validationErrors.push('Critical priority requires High risk or High impact');
42    g_form.showFieldMsg('priority', 'Critical priority requires high risk/impact', 'error');
43  }
44
45  // Validation 3: Approval requirements
46  var type = g_form.getValue('type');
47  var cabRequired = g_form.getValue('cab_required');
48  var approvalSet = g_form.getValue('approval');
49
50  // Standard and Emergency changes require CAB approval
51  if ((type === 'standard' || type === 'emergency') && cabRequired === 'false') {
52    validationErrors.push('CAB approval is required for ' + type + ' changes');
53    g_form.showFieldMsg('cab_required', 'Required for this change type', 'error');
54  }
55
56  // Validation 4: Assignment validation
57  var assignmentGroup = g_form.getValue('assignment_group');
58  var assignedTo = g_form.getValue('assigned_to');
59  var state = g_form.getValue('state');
60
61  // In Progress state requires assignment
62  if (state === '2' && !assignedTo) {  // In Progress
63    validationErrors.push('Changes in progress must be assigned to a user');
64    g_form.showFieldMsg('assigned_to', 'Required when state is In Progress', 'error');
65  }
66
67  // Assigned user must be member of assignment group
68  if (assignmentGroup && assignedTo) {
69    // This would need a GlideAjax call for real-time validation
70    // Simplified version shown here
71    // var isMember = checkGroupMembership(assignedTo, assignmentGroup);
72  }
73
74  // Validation 5: Financial validation
75  var estimatedCost = parseFloat(g_form.getValue('u_estimated_cost')) || 0;
76  var approvalLevel = g_form.getValue('u_approval_level');
77
78  // Changes over $10,000 require executive approval
79  if (estimatedCost > 10000 && approvalLevel !== 'executive') {
80    validationErrors.push('Changes over $10,000 require executive approval');
81    g_form.showFieldMsg('u_approval_level', 'Executive approval required for cost > $10k', 'error');
82  }
83
84  // Validation 6: Required field combinations
85  var implementationPlan = g_form.getValue('implementation_plan');
86  var backoutPlan = g_form.getValue('backout_plan');
87  var testPlan = g_form.getValue('test_plan');
88
89  // All three plans required for production changes
90  var environment = g_form.getValue('u_environment');
91  if (environment === 'production') {
92    if (!implementationPlan) {
93      validationErrors.push('Implementation plan required for production changes');
94      g_form.showFieldMsg('implementation_plan', 'Required for production', 'error');
95    }
96    if (!backoutPlan) {
97      validationErrors.push('Backout plan required for production changes');
98      g_form.showFieldMsg('backout_plan', 'Required for production', 'error');
99    }
100    if (!testPlan) {
101      validationErrors.push('Test plan required for production changes');
102      g_form.showFieldMsg('test_plan', 'Required for production', 'error');
103    }
104  }
105
106  // Show all errors in alert if validation failed
107  if (validationErrors.length > 0) {
108    var errorMessage = 'Please fix the following validation errors:\n\n';
109    errorMessage += validationErrors.map(function(error, index) {
110      return (index + 1) + '. ' + error;
111    }).join('\n');
112
113    alert(errorMessage);
114    return false;  // Prevent form submission
115  }
116
117  // All validations passed
118  return true;
119}

How to Use

1. Create an onSubmit Client Script on your table 2. Customize validation rules for your business requirements 3. Update field names to match your table structure 4. Add or remove validation rules as needed 5. Test all validation scenarios thoroughly 6. Consider performance impact of complex validations 7. For server-side validation, duplicate logic in Business Rules

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts