Skip to content

Commit e393bae

Browse files
Create Set due date on RITM
Sets the "due_date" for a Request Item (RITM) based on delivery time set on catalog item record. Follows the below step to calculate and set value: 1) Reading the delivery time (in days) from the associated Catalog Item 2) Parsing the days component from the display value (e.g. "5 Days" - 5). 3) Adding those days to the record's creation date ("sys_created_on"). 4) Writing the calculated date to "due_date".
1 parent f900211 commit e393bae

File tree

1 file changed

+39
-0
lines changed
  • Server-Side Components/Business Rules/Set Due Date on Request and RITM based on Delivery Time

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Business Rule: Set due date on RITM
3+
* Table: sc_req_item
4+
* When: before insert
5+
*
6+
* @description
7+
* Sets the "due_date" for a Request Item (RITM) by:
8+
* 1) Reading the delivery time (in days) from the associated Catalog Item
9+
* 2) Parsing the days component from the display value (e.g. "5 Days" - 5).
10+
* 3) Adding those days to the record's creation date ("sys_created_on").
11+
* 4) Writing the calculated date to "due_date".
12+
*/
13+
(function executeRule(current, previous /* null when async */) {
14+
15+
try {
16+
// Get days from Catalog Item's delivery time field.
17+
var deliveryTimeDaysStr = current.getDisplayValue('cat_item.delivery_time').split(' ')[0];
18+
19+
// Convert to a Number
20+
var deliveryTimeDays = Number(deliveryTimeDaysStr);
21+
22+
var createdDate = current.getValue('sys_created_on');
23+
var newDueDate = new GlideDateTime(createdDate);
24+
25+
// Add delivery days to created date in UTC to avoid timezone drift.
26+
newDueDate.addDaysUTC(deliveryTimeDays);
27+
28+
// Set due date
29+
current.due_date.setValue(newDueDate.getValue());
30+
31+
// If you also need the estimated delivery date, uncomment the line below:
32+
// current.estimated_delivery.setValue(newDueDate.getValue());
33+
34+
} catch (ex) {
35+
var message = ex.getMessage();
36+
gs.error("Error in BR: Set Due Date on RITM - " + message);
37+
}
38+
39+
})(current, previous);

0 commit comments

Comments
 (0)