Duration Calculation on SCTASK

Mandadi
Giga Contributor

Hi Developers,

I need the Duration field on sc_task table to display the time difference between Created on and closed fields. I have tried below code in before Business Rule with condition when closed changes.

current.calendar_duration = GlideDateTime.subtract(new GlideDateTime(current.getValue("sys_created_on")), new GlideDateTime(current.getValue("closed_at")));
current.update();

Also, i tried with Client script and Script include. When this field is on form, it is displayed with duration and when hidden from form, no value is record in duration field.

Note: I need duration field to be recorded with time when the task is closed though it is not displayed on form view.

Could someone help me in solving this issue?

Thanks in advance.

1 ACCEPTED SOLUTION

Mandadi
Giga Contributor

It worked out for me with same code.. only modification in this is i have changed Before  Business to after BR.

View solution in original post

4 REPLIES 4

prasannaposetty
Tera Expert

Instead of using .subtract, you can use dateDiff function in servicenow to calculate difference between duration fields.

https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_GS-dateDiff_S_S_B

thanks for you response Prasanna, it didn't worked out in my case.

amlanpal
Kilo Sage

Hi,

 

You might find this post helpful in your case. The similar requirement was achieved here:

https://community.servicenow.com/community?id=community_question&sys_id=d2c3c7a5dbd8dbc01dcaf3231f96190a

 

I hope this helps.Please mark correct/helpful based on impact

Mandadi
Giga Contributor

It worked out for me with same code.. only modification in this is i have changed Before  Business to after BR.