---
original_url: "https://jace.pro/blog/set-duration-and-business-duration/"
format: markdown
ai_optimized: true
---

Set Duration and Business Duration# Set Duration and Business Duration

October 14, 2017 [servicenow](/tags/servicenow/)

  Enable AI AnimationI had to add some code to set this for some reporting needs. This should be part of the offering ServiceNow provides.

Below is my business rule, and fix script to catch new records and fix
old records.

`// Business Rule
// Table: Task [task]
// Active: true
// Advanced: true
// When: Before
// Order 100000000
// Insert: true
// Update:true
// Condition:
// calendar_durationISEMPTY^
// opened_atISNOTEMPTY^
// closed_atISNOTEMPTY^
// sys_class_name!=incident^
// sys_class_name!=problem
//
// Script
var opened = current.opened_at.getDisplayValue();
var closed = current.closed_at.getDisplayValue();
current.calendar_duration = gs.dateDiff(opened, closed, false);
current.business_duration = gs.calDateDiff(opened, closed, false);`Here’s my fix script.

`var log = 'calc. old durations';
var query = '';
query += 'calendar_durationISEMPTY^';
query += 'opened_atISNOTEMPTY^';
query += 'closed_atISNOTEMPTY^';
query += 'sys_class_name!=incident^';
query += 'sys_class_name!=problem^';

var task = new GlideRecord('task');
task.addEncodedQuery(query);
task.query();
var x = 0;
while(task.next()){
  x++;
  if(x%1000 === 0){
    gs.log('x: ' + x + ' of ' + task.getRowCount(), log);
  }
  var opened = task.opened_at.getDisplayValue();
  var closed = task.closed_at.getDisplayValue();
  task.business_duration = gs.calDateDiff(opened, closed, false);
  task.calendar_duration = gs.dateDiff(opened, closed, false);
  task.setWorkflow(false);
  task.autoSysFields(false);
  task.update();
}`
---
[View this page on GitHub](https://github.com/jacebenson/jace.pro/tree/main/./src/posts/2017/2017-10-13-set-duration-and-business-duration.md).

[Set Duration and Business Duration](https://jace.pro/blog/set-duration-and-business-duration/) [Jace Benson](https://jace.pro) ![Jace Benson](https://jace.pro/icon-512x512.png)

---

*This content is from Jace Benson's ServiceNow and tech blog at jace.pro*
*Original post: https://jace.pro/blog/set-duration-and-business-duration/*
