Skip to Content

Calculate and Update SLA

Automatically calculate SLA due dates based on priority, business hours, and update SLA status.

Table: incident When: before
#sla #due-date #schedule #calculation #before #insert #update #business-hours

Script Code

JavaScript
1(function executeRule(current, previous /*null when async*/) {
2
3  // Configuration: Define SLA response times by priority (in hours)
4  var slaResponseTimes = {
5    '1': 1,    // Critical: 1 hour
6    '2': 4,    // High: 4 hours
7    '3': 8,    // Moderate: 8 hours
8    '4': 24,   // Low: 24 hours
9    '5': 48    // Planning: 48 hours
10  };
11
12  // Configuration: Define SLA resolution times by priority (in hours)
13  var slaResolutionTimes = {
14    '1': 4,    // Critical: 4 hours
15    '2': 8,    // High: 8 hours
16    '3': 24,   // Moderate: 24 hours
17    '4': 72,   // Low: 72 hours
18    '5': 120   // Planning: 120 hours
19  };
20
21  // Get the default schedule sys_id (8-5 weekdays excluding holidays)
22  var scheduleId = 'c6a2389e0a0a0b9b006cfc04227be354';
23
24  // Only calculate SLA on new records or when priority changes
25  if (current.isNewRecord() || current.priority.changes()) {
26
27    var priority = current.priority.toString();
28    var responseHours = slaResponseTimes[priority];
29    var resolutionHours = slaResolutionTimes[priority];
30
31    // Calculate response due date
32    if (responseHours && current.isNil('u_response_due')) {
33      var schedule = new GlideSchedule(scheduleId);
34      var startTime = new GlideDateTime();
35
36      // Add business hours to current time
37      var duration = new GlideDuration(responseHours * 60 * 60 * 1000);  // Convert to ms
38      var responseDue = schedule.add(startTime, duration);
39
40      current.u_response_due = responseDue;
41      gs.info('Set response SLA due date for incident ' + current.number + ': ' + responseDue.getDisplayValue());
42    }
43
44    // Calculate resolution due date
45    if (resolutionHours && (current.isNewRecord() || current.priority.changes())) {
46      var schedule = new GlideSchedule(scheduleId);
47      var startTime = new GlideDateTime();
48
49      var duration = new GlideDuration(resolutionHours * 60 * 60 * 1000);
50      var resolutionDue = schedule.add(startTime, duration);
51
52      current.u_resolution_due = resolutionDue;
53      gs.info('Set resolution SLA due date for incident ' + current.number + ': ' + resolutionDue.getDisplayValue());
54    }
55  }
56
57  // Check SLA breach status
58  if (!current.isNewRecord()) {
59    var now = new GlideDateTime();
60
61    // Check response SLA breach
62    if (!current.u_response_due.nil() && current.isNil('u_responded_at')) {
63      var responseDue = new GlideDateTime(current.u_response_due);
64      if (now.after(responseDue)) {
65        current.u_response_sla_breached = true;
66        current.work_notes = 'Response SLA breached at ' + now.getDisplayValue();
67      }
68    }
69
70    // Check resolution SLA breach
71    if (!current.u_resolution_due.nil() && current.state.toString() !== '6') {
72      var resolutionDue = new GlideDateTime(current.u_resolution_due);
73      if (now.after(resolutionDue)) {
74        current.u_resolution_sla_breached = true;
75        current.work_notes = 'Resolution SLA breached at ' + now.getDisplayValue();
76      }
77    }
78
79    // Mark SLA as met when incident is resolved within timeframe
80    if (current.state.changesTo('6')) {  // Resolved
81      if (!current.u_resolution_sla_breached.toString() === 'true') {
82        current.u_sla_met = true;
83      }
84    }
85  }
86
87})(current, previous);

How to Use

1. Create a before Business Rule on your table 2. Check "Insert" and "Update" checkboxes 3. Create custom fields: u_response_due, u_resolution_due, u_response_sla_breached, u_resolution_sla_breached, u_responded_at, u_sla_met 4. Update schedule sys_id to match your business hours 5. Customize SLA times based on your organization's requirements 6. Consider using ServiceNow's built-in SLA engine for production use

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts