← Script library

Client Scripts

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.

JavaScript
function onSubmit() {
  // Configuration: Define validation rules
  var validationErrors = [];

  // Validation 1: Date range validation
  var startDate = g_form.getValue('start_date');
  var endDate = g_form.getValue('end_date');

  if (startDate && endDate) {
    var start = new Date(startDate);
    var end = new Date(endDate);

    if (end < start) {
      validationErrors.push('End date must be after start date');
      g_form.showFieldMsg('end_date', 'Must be after start date', 'error');
    }

    // Check minimum duration (e.g., at least 1 hour)
    var durationMs = end - start;
    var durationHours = durationMs / (1000 * 60 * 60);

    if (durationHours < 1) {
      validationErrors.push('Change window must be at least 1 hour');
      g_form.showFieldMsg('end_date', 'Minimum 1 hour duration required', 'error');
    }
  }

  // Validation 2: Risk and impact correlation
  var risk = g_form.getValue('risk');
  var impact = g_form.getValue('impact');
  var priority = g_form.getValue('priority');

  // High risk must have high/medium impact
  if (risk === '1' && (impact === '3' || impact === '4')) {  // High risk, Low impact
    validationErrors.push('High risk changes must have High or Medium impact');
    g_form.showFieldMsg('impact', 'High risk requires higher impact rating', 'error');
  }

  // Critical priority requires high risk or high impact
  if (priority === '1' && risk !== '1' && impact !== '1') {
    validationErrors.push('Critical priority requires High risk or High impact');
    g_form.showFieldMsg('priority', 'Critical priority requires high risk/impact', 'error');
  }

  // Validation 3: Approval requirements
  var type = g_form.getValue('type');
  var cabRequired = g_form.getValue('cab_required');
  var approvalSet = g_form.getValue('approval');

  // Standard and Emergency changes require CAB approval
  if ((type === 'standard' || type === 'emergency') && cabRequired === 'false') {
    validationErrors.push('CAB approval is required for ' + type + ' changes');
    g_form.showFieldMsg('cab_required', 'Required for this change type', 'error');
  }

  // Validation 4: Assignment validation
  var assignmentGroup = g_form.getValue('assignment_group');
  var assignedTo = g_form.getValue('assigned_to');
  var state = g_form.getValue('state');

  // In Progress state requires assignment
  if (state === '2' && !assignedTo) {  // In Progress
    validationErrors.push('Changes in progress must be assigned to a user');
    g_form.showFieldMsg('assigned_to', 'Required when state is In Progress', 'error');
  }

  // Assigned user must be member of assignment group
  if (assignmentGroup && assignedTo) {
    // This would need a GlideAjax call for real-time validation
    // Simplified version shown here
    // var isMember = checkGroupMembership(assignedTo, assignmentGroup);
  }

  // Validation 5: Financial validation
  var estimatedCost = parseFloat(g_form.getValue('u_estimated_cost')) || 0;
  var approvalLevel = g_form.getValue('u_approval_level');

  // Changes over $10,000 require executive approval
  if (estimatedCost > 10000 && approvalLevel !== 'executive') {
    validationErrors.push('Changes over $10,000 require executive approval');
    g_form.showFieldMsg('u_approval_level', 'Executive approval required for cost > $10k', 'error');
  }

  // Validation 6: Required field combinations
  var implementationPlan = g_form.getValue('implementation_plan');
  var backoutPlan = g_form.getValue('backout_plan');
  var testPlan = g_form.getValue('test_plan');

  // All three plans required for production changes
  var environment = g_form.getValue('u_environment');
  if (environment === 'production') {
    if (!implementationPlan) {
      validationErrors.push('Implementation plan required for production changes');
      g_form.showFieldMsg('implementation_plan', 'Required for production', 'error');
    }
    if (!backoutPlan) {
      validationErrors.push('Backout plan required for production changes');
      g_form.showFieldMsg('backout_plan', 'Required for production', 'error');
    }
    if (!testPlan) {
      validationErrors.push('Test plan required for production changes');
      g_form.showFieldMsg('test_plan', 'Required for production', 'error');
    }
  }

  // Show all errors in alert if validation failed
  if (validationErrors.length > 0) {
    var errorMessage = 'Please fix the following validation errors:\n\n';
    errorMessage += validationErrors.map(function(error, index) {
      return (index + 1) + '. ' + error;
    }).join('\n');

    alert(errorMessage);
    return false;  // Prevent form submission
  }

  // All validations passed
  return true;
}

How to use it

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

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.