diff --git a/Server-Side Components/Business Rules/Set Due Date on Request and RITM based on Delivery Time/README.md b/Server-Side Components/Business Rules/Set Due Date on Request and RITM based on Delivery Time/README.md new file mode 100644 index 0000000000..ed640ed743 --- /dev/null +++ b/Server-Side Components/Business Rules/Set Due Date on Request and RITM based on Delivery Time/README.md @@ -0,0 +1,21 @@ +**Use Case:** + +When using generic request flows for catalog items, the catalog items have different delivery time. However, flow stages are not flexible enough to dynamically set the due dates based on catalog item's delivery time. This limitation can lead to inaccurate due dates, affecting SLAs and user expectations. + +**Proposed Solution:** + +To address this, create two business rules to set due dates of RITM and Request: +1. Requested Item (RITM) Due Date: + - Implement a Before Business Rule on the "sc_req_item" table. + - This rule will calculate the due date by adding the delivery time (in days) from the associated catalog item record to the current date. + - The calculated due date will then be set on the RITM. + +2. Request Due Date: + - Implement a Before Business Rule on the "sc_request" table. + - It will then determine the maximum (farthest) due date among all RITMs associated with the request and set it as the due date for the request. + - Since RITMs are created before the request record, this logic ensures accurate aggregation of due dates. + +**Benefits:** +- Ensures accurate due dates for both RITMs and Requests. +- Enhances user satisfaction by aligning expectations with actual delivery timelines. +- Reduces manual intervention and customization in flows. diff --git a/Server-Side Components/Business Rules/Set Due Date on Request and RITM based on Delivery Time/set_due_date_on_request.js b/Server-Side Components/Business Rules/Set Due Date on Request and RITM based on Delivery Time/set_due_date_on_request.js new file mode 100644 index 0000000000..0ac968dbaf --- /dev/null +++ b/Server-Side Components/Business Rules/Set Due Date on Request and RITM based on Delivery Time/set_due_date_on_request.js @@ -0,0 +1,31 @@ +/** + * Business Rule: Set due date on Request + * Table: sc_request + * When: before insert + * + * @description + * Sets the "due_date" on a Request by: + * 1) Finding the associated RITM with the highest "due_date". + * 2) Assigning the Request's "due_date" to match that RITM's "due_date". + */ +(function executeRule(current, previous /* null when async */) { + + try { + // Query RITMs linked to this Request and pick the one with the highest due_date. + var reqGR = new GlideRecord("sc_req_item"); + reqGR.addQuery("request", current.getUniqueValue()); + reqGR.orderByDesc('due_date'); // Latest due_date first + reqGR.setLimit(1); + reqGR.query(); + + if (reqGR.next()) { + // Set due date + current.setValue('due_date', reqGR.getValue('due_date')); + } + + } catch (ex) { + var message = ex.getMessage(); + gs.error("Business Rule: Error - " + message); + } + +})(current, previous); diff --git a/Server-Side Components/Business Rules/Set Due Date on Request and RITM based on Delivery Time/set_due_date_on_ritm.js b/Server-Side Components/Business Rules/Set Due Date on Request and RITM based on Delivery Time/set_due_date_on_ritm.js new file mode 100644 index 0000000000..234107e8a6 --- /dev/null +++ b/Server-Side Components/Business Rules/Set Due Date on Request and RITM based on Delivery Time/set_due_date_on_ritm.js @@ -0,0 +1,39 @@ +/** + * Business Rule: Set due date on RITM + * Table: sc_req_item + * When: before insert + * + * @description + * Sets the "due_date" for a Request Item (RITM) by: + * 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". + */ +(function executeRule(current, previous /* null when async */) { + + try { + // Get days from Catalog Item's delivery time field. + var deliveryTimeDaysStr = current.getDisplayValue('cat_item.delivery_time').split(' ')[0]; + + // Convert to a Number + var deliveryTimeDays = Number(deliveryTimeDaysStr); + + var createdDate = current.getValue('sys_created_on'); + var newDueDate = new GlideDateTime(createdDate); + + // Add delivery days to created date in UTC to avoid timezone drift. + newDueDate.addDaysUTC(deliveryTimeDays); + + // Set due date + current.due_date.setValue(newDueDate.getValue()); + + // If you also need the estimated delivery date, uncomment the line below: + // current.estimated_delivery.setValue(newDueDate.getValue()); + + } catch (ex) { + var message = ex.getMessage(); + gs.error("Error in BR: Set Due Date on RITM - " + message); + } + +})(current, previous);