diff --git a/Server-Side Components/Business Rules/Calculate Incident Duration and Validation/Duration.js b/Server-Side Components/Business Rules/Calculate Incident Duration and Validation/Duration.js new file mode 100644 index 0000000000..4860ad3869 --- /dev/null +++ b/Server-Side Components/Business Rules/Calculate Incident Duration and Validation/Duration.js @@ -0,0 +1,36 @@ +(function executeRule(current, previous /*null when async*/ ) { + + /* + Inputs + 1. Opened(opened_at) + 2. Resolved(resolved_at) + + Checks & Validation + 1. It will check the Date/Time validation whether it Resolved time should be greater than Open time + 2. And it will not calculate the empty values + + Outputs + 1. If everthing has a right value like the Opened < Resolve Date/Time then Duration will be calculated and populated in the respective field + 2. Negative case if Opened > Resolved the duration will not calculate and it will Abort the action to save the record with Error message. + */ + + // Getting both the date from the record + var opened = GlideDateTime(current.opened_at.getDisplayValue()); + var resolved = GlideDateTime(current.resolved_at.getDisplayValue()); + + //If the opened and resolved times are present, calculate the duration + if (opened && resolved) { + var difference = GlideDateTime.subtract(opened, resolved); + if (difference.getYearUTC() >= 1970) + current.calendar_duration.setValue(difference); + else { + current.calendar_duration.setValue(null); + current.resolved_at.setValue(null); + current.setAbortAction(true); + gs.addErrorMessage("Incident Resolve date/time must be greater than incident Opened date/time"); + } + } + + + +})(current, previous); \ No newline at end of file diff --git a/Server-Side Components/Business Rules/Calculate Incident Duration and Validation/Readme.md b/Server-Side Components/Business Rules/Calculate Incident Duration and Validation/Readme.md new file mode 100644 index 0000000000..d870bbac1b --- /dev/null +++ b/Server-Side Components/Business Rules/Calculate Incident Duration and Validation/Readme.md @@ -0,0 +1,11 @@ +Calculate Incident Duration and Validation. + +Script Type : Business Rule Trigger: before update Table: incident Condition: Resolved Changes or Opened Changes + +Goal : To calculate the duration of a particular record and how much time has been spent on a particular ticket. + +Walk through of code : +So when the Resolved Changes or Opened Changes in a particular record to calculate the duration will this Business rule will pull those values +And then check whether the Opened Data/Time is lesser than the Resolved Date/Time the will calculate the duration +Else it will throw the Error Message and then Abort that action and won't save the record and will clear the values. +