Skip to content

Commit 0a1c248

Browse files
authored
Task Due date calculation based on priority (ServiceNowDevProgram#741)
* Create code.js * Create readme.md * Update readme.md * Update readme.md * Update readme.md
1 parent f932b62 commit 0a1c248

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Auto-calculate Due Date based on priority Business Rule (Before BR)
2+
// This business rule calculates the due date based on task priority.
3+
4+
(function executeRule(current, previous /*, display*/) {
5+
6+
// Define priority-to-due-date mapping (in hours)
7+
var priorityToDueDate = {
8+
1: 4, // High priority: Due in 4 hours
9+
2: 24, // Medium priority: Due in 24 hours
10+
3: 72 // Low priority: Due in 72 hours
11+
};
12+
13+
// Get the priority value from the task
14+
var priority = current.priority;
15+
16+
// Check if priority is valid and mapping exists
17+
if (priority && priorityToDueDate.hasOwnProperty(priority)) {
18+
// Calculate the due date
19+
var dueDate = new GlideDateTime();
20+
dueDate.addHours(priorityToDueDate[priority]);
21+
22+
// Update the due date field
23+
current.due_date = dueDate;
24+
}
25+
26+
})(current, previous);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Objective
2+
This ServiceNow business rule script is designed to automatically calculate the due date for a task based on its priority. It executes before the record is saved (Before Business Rule) and calculates the due date in hours, depending on the priority level of the task.
3+
4+
# Priority-to-Due-Date Mapping
5+
```
6+
var priorityToDueDate = {
7+
1: 4, // High priority: Due in 4 hours
8+
2: 24, // Medium priority: Due in 24 hours
9+
3: 72 // Low priority: Due in 72 hours
10+
};
11+
```
12+
This section defines a JavaScript object called priorityToDueDate that maps priority values to due date intervals in hours. For example, if the task has a priority of 1 (High), its due date will be set to 4 hours from the current date and time.
13+
Alternatively, we can store these mapping values in a custom table, allowing us to update them as necessary.
14+
15+
# Get Priority Value
16+
```
17+
var priority = current.priority;
18+
```
19+
This line retrieves the priority value from the current record and stores it in a variable called priority.
20+
21+
# Check Priority Validity and Mapping
22+
```
23+
if (priority && priorityToDueDate.hasOwnProperty(priority)) {
24+
// Code goes here
25+
}
26+
```
27+
This if statement checks if the priority variable is defined (not null or undefined) and if it exists as a key in the priorityToDueDate mapping. This ensures that the priority value is valid and has a corresponding due date interval.
28+
29+
# Calculate Due Date
30+
```
31+
var dueDate = new GlideDateTime();
32+
dueDate.addHours(priorityToDueDate[priority]);
33+
```
34+
If the priority is valid, a new GlideDateTime object is created, and the addHours method is used to add the appropriate number of hours (based on the priority) to the current date and time. This calculates the due date.
35+
36+
# Update Due Date Field
37+
```
38+
current.due_date = dueDate;
39+
```
40+
Finally, the due_date field in the current record is updated with the calculated due date.

0 commit comments

Comments
 (0)