← Script library

Client Scripts

Conditional Mandatory Fields

Make fields mandatory based on the value of another field (e.g., require close notes only when state is Resolved).

JavaScript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  // Exit if form is loading
  if (isLoading) {
    return;
  }

  // Configuration: Define your conditions
  // Change these values to match your requirements
  var resolvedState = '6';        // The state value that triggers mandatory fields
  var mandatoryFields = [         // Fields to make mandatory
    'close_code',
    'close_notes'
  ];

  // Check if the new state matches the resolved state
  if (newValue === resolvedState) {
    // Make fields mandatory
    mandatoryFields.forEach(function(fieldName) {
      g_form.setMandatory(fieldName, true);
    });

    // Optional: Show info message to user
    g_form.addInfoMessage('Close code and close notes are required for resolved incidents.');

  } else {
    // Remove mandatory when state changes back
    mandatoryFields.forEach(function(fieldName) {
      g_form.setMandatory(fieldName, false);
    });
  }
}

How to use it

1. Create an onChange Client Script on your table 2. Set the field to the one that controls the mandatory logic 3. Update `resolvedState` and `mandatoryFields` for your use case 4. Optionally add an onLoad script to handle when form first opens

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