How to calculate "Duration" for task.

hamar
Tera Contributor

I want to calculate "duration" for task"
For incident, we can calculate "duration", but I can't calculate it for task, like this pic. (brank)

hamar_0-1699938319604.png

Please tell me why I can't calculate it for task and how to calculate it for task.

 

Thanks

 

3 REPLIES 3

Amit Gujarathi
Giga Sage
Giga Sage

HI @hamar ,
I trust you are doing great.
Please refer below code for the same

(function executeRule(current) {
  var startDate = current.getValue('start_date'); // Replace with your actual start date field
  var endDate = current.getValue('end_date'); // Replace with your actual end date field

  if (startDate && endDate) {
    var durationInMillis = endDate - startDate;
    var durationInSeconds = durationInMillis / 1000;
    var durationInMinutes = durationInSeconds / 60;

    current.setValue('duration', durationInMinutes);
  }
})(current);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Thank you for your reply.
Sorry for being beginner, but I'd like to know where I should type this code.
I couldn't find.

 

thanks

Hi @hamar the reason for the not calcuating is that there is no Business rules configured for the task .Please find the OOB Business rules configured for calculating Duration.

EswararaoChapp_0-1699939302908.png

https://XXXXXXXX.service-now.com/sys_script_list.do?sysparm_query=scriptLIKEcalendar_duration&syspar...

You have to create similar kind of Business rule for the table where you need to auto populate duaration value by updating the conditions.

 

Cheers, hope that helps

Eswar Chappa

*** Please mark as "Correct" or "Helpful" as appropriate ***