← Script library

Business Rules

Cascade Status Updates

Automatically update related records when the parent record status changes (e.g., resolve all child tasks when parent is resolved).

JavaScript
(function executeRule(current, previous /*null when async*/) {

  // Only process when state changes to Resolved or Closed
  var resolvedStates = ['6', '7'];  // 6=Resolved, 7=Closed

  if (!current.state.changes() || resolvedStates.indexOf(current.state.toString()) === -1) {
    return;
  }

  // Configuration: Define related tables and relationships
  var relatedTables = [
    {
      table: 'sc_task',
      parentField: 'request_item',  // Field that links to parent
      statusField: 'state',
      statusValue: '3'  // Closed Complete
    },
    {
      table: 'incident_task',
      parentField: 'incident',
      statusField: 'state',
      statusValue: '3'  // Closed Complete
    }
  ];

  // Process each related table
  relatedTables.forEach(function(config) {
    var grRelated = new GlideRecord(config.table);
    grRelated.addQuery(config.parentField, current.sys_id);
    grRelated.addQuery('active', 'true');  // Only update active records
    grRelated.query();

    var updatedCount = 0;
    while (grRelated.next()) {
      // Set the status
      grRelated.setValue(config.statusField, config.statusValue);

      // Add work note explaining the cascade
      grRelated.work_notes = 'Automatically closed due to parent ' +
                             current.number + ' being resolved';

      // Update the record
      grRelated.update();
      updatedCount++;
    }

    if (updatedCount > 0) {
      gs.info('Cascaded status update: closed ' + updatedCount + ' ' +
              config.table + ' records for ' + current.number);

      // Optional: Add work note to parent
      current.work_notes = 'Automatically closed ' + updatedCount +
                          ' related ' + config.table + ' records';
    }
  });

})(current, previous);

How to use it

1. Create an after Business Rule on your parent table 2. Check "Update" checkbox 3. Customize the `relatedTables` configuration 4. Test with parent and child records

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