Business Rule to calculate Duration using Date/Time field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2024 07:04 AM
Hi, I have the fields of type Date/Time:
1) u_starting_time
2) u_ending_time
In addition I have the fields of type Duration:
1) u_execution_time
2) u_actual_time
I need to write a BR that works on a certain condition and check:
(u_starting_time + u_execution_time) - u_ending_time > 30 minutes
If yes, I need to populate the field u_actual_time of type Duration with the difference:
(u_starting_time + u_execution_time) - u_ending_time
For example:
u_starting_time = 05/11/2024 1:00PM
u_executin_time = 0 days 0 hours 30 minutes
u_ending_time = 5/11/2024 2:10PM
(u_starting_time + u_execution_time) - u_ending_time = 40minutes (which is bigger than 30min)
so in this case I will need to populate u_actual_time:
0 days 0 hours 40minutes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2024 10:32 AM - edited 05-11-2024 01:37 PM
Have you reviewed OOB business rules that calculate a duration? Go to a list view of BRs and filter "name", "contains", "duration". I have 35 in my PDI.
The following logic in a BR that sets a duration field based on two date-time fields follows:
// Add your code here
var sDT = new GlideDateTime(current.u_start_date);
var eDT = new GlideDateTime(current.u_end_date);
var dur = eDT.getNumericValue() - sDT.getNumericValue();
gs.addInfoMessage("Duration is " + dur);
current.u_duration = new GlideDuration(dur);
gs.addInfoMessage("Field is " + current.u_duration);
I hope this helps.
[edit] Trying to add logic as you describe "(u_starting_time + u_execution_time) - u_ending_time > 30 minutes, then set actual time to the calculated duration"
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var sDT = new GlideDateTime(current.u_start_date); // my date-time field
var eDT = new GlideDateTime(current.u_end_date); // my date-time field
var eT = new GlideDateTime(current.u_execution_time); // duration field
var dur = (sDT.getNumericValue() + eT.getNumericValue()) - eDT.getNumericValue();
gs.addInfoMessage("Duration is " + dur);
if (dur > (30*60 *1000)) {
current.u_actual_time = new GlideDuration(dur);
gs.addInfoMessage("Setting u_actual_time = " + current.u_actual_time);
}
})(current, previous);
You can get negative values for the calculated duration based on your condition.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2024 11:10 AM
Hi @Alon Grod ,
Try below script in business rule
function executeRule(current, previous /*null when async*/) {
var starting_time = new GlideDateTime(current.u_starting_time);
var ending_time = new GlideDateTime(current.u_ending_time);
var execution_time = current.u_execution_time.getDurationValue(); starting_time.addSeconds(execution_time); difference = starting_time.getNumericValue() - ending_time.getNumericValue();
difference_in_minutes = difference / 1000 / 60;
if (difference_in_minutes > 30) current.u_actual_time = difference;
} })(current, previous);
Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand