Skip to Content

Cascade Status Updates

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

Table: incident When: after
#cascade #related-records #status-update #after #update #automation

Script Code

JavaScript
1(function executeRule(current, previous /*null when async*/) {
2
3  // Only process when state changes to Resolved or Closed
4  var resolvedStates = ['6', '7'];  // 6=Resolved, 7=Closed
5
6  if (!current.state.changes() || resolvedStates.indexOf(current.state.toString()) === -1) {
7    return;
8  }
9
10  // Configuration: Define related tables and relationships
11  var relatedTables = [
12    {
13      table: 'sc_task',
14      parentField: 'request_item',  // Field that links to parent
15      statusField: 'state',
16      statusValue: '3'  // Closed Complete
17    },
18    {
19      table: 'incident_task',
20      parentField: 'incident',
21      statusField: 'state',
22      statusValue: '3'  // Closed Complete
23    }
24  ];
25
26  // Process each related table
27  relatedTables.forEach(function(config) {
28    var grRelated = new GlideRecord(config.table);
29    grRelated.addQuery(config.parentField, current.sys_id);
30    grRelated.addQuery('active', 'true');  // Only update active records
31    grRelated.query();
32
33    var updatedCount = 0;
34    while (grRelated.next()) {
35      // Set the status
36      grRelated.setValue(config.statusField, config.statusValue);
37
38      // Add work note explaining the cascade
39      grRelated.work_notes = 'Automatically closed due to parent ' +
40                             current.number + ' being resolved';
41
42      // Update the record
43      grRelated.update();
44      updatedCount++;
45    }
46
47    if (updatedCount > 0) {
48      gs.info('Cascaded status update: closed ' + updatedCount + ' ' +
49              config.table + ' records for ' + current.number);
50
51      // Optional: Add work note to parent
52      current.work_notes = 'Automatically closed ' + updatedCount +
53                          ' related ' + config.table + ' records';
54    }
55  });
56
57})(current, previous);

How to Use

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

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts