Incident Time worked

MK21
Tera Contributor

Hi all.

 

I have a requirement, I created in the custom table for the dummy data uploaded in the system, we have incident created time and resolved time , need to calculate the worked time, for that I created the time worked field, how to achieve this?

MK21_0-1738317892753.png

 

12 REPLIES 12

KoteswaraVM
Tera Expert

Hi @MK21 ,


In ServiceNow we use Use the GlideDateTime methods to perform date-time operations, such as instantiating a GlideDateTime object, performing date-time calculations, formatting a date-time, or converting between date-time formats. Link FYR -  GlideDateTime - Global - Product Documentation - ServiceNow 

Please try to use the GlideDateTime object to achieve the requirement to get the duration between two dates.

Also, Please find the script below for your reference.

var actualDateTime = new GlideDateTime();

actualDateTime.setDisplayValue(gs.nowDateTime());

var gr = new GlideRecord('change_request');
gr.addQuery('number', 'CHG0000015');
gr.query();
if (gr.next()) {

    var createdTime = gr.sys_created_on.getDisplayValue(); //implementationEndTime
    gs.print(actualDateTime);
    dur = gs.dateDiff(createdTime, actualDateTime);

    var dateDifference = new GlideDuration(dur);
    gs.print("dateDifference : " + dateDifference);
    var d = dateDifference.getDayPart();
    gs.print("d :" + d);
}


The output for the reference script is in count of days. Please change the script according to your requirement based upon the Time_Worked field type.

 

 

I hope this will help. Please mark my response helpful if it worked.




Thanks,
Koteswara Vara Prasad M

WBit
Tera Contributor

Can you clarify your requirement? I have just started learning Platform Analytics, so maybe I can help. I just don't really understand the 'time worked' you mention. It's a bit confusing.

jMarshal
Mega Sage
Mega Sage

@MK21  -- unfortunately, "date math" can be quite tricky (read: nuanced, not "difficult")...and when specific requirements are not clear and complete, it gets hard (actually "difficult"). This is why Performance Analytics exists...they take the nuance out of these things, but PA is complex in and of itself, so you need to invest time in that specific module, to understand that nuance, to ultimately accomplish your goals (IE. - provide a solution to a client or complete a test).

In this case, I think some commentors are just indicating that the "requirements" for calculating "time worked" can't be complete and clear with any fields described as "reported date/time" and "resolved date/time".

However, I do understand that your challenge could be less about what you're trying to achieve (knowing the "actual time worked" on an incident) -- and more about what you would do if given a "date math" requirement, or perhaps your approach to difficult challenge.

I have a few thoughts on this, which might help. Firstly, you may want to consider @Mark Manders input and include in your response/solution, a caveat that the challenge is flawed at root. IE - "date resolved minus date reported in no way equals time worked on incident"...

...having said that, you can then describe a method for the date math, which is very rudimentary and is predicated on the idea that "time worked DOES equate to date/time resolved minus date/time reported" (which you've already established that it hasn't).

...and I would do that by using an onclose br, which calls a script include, which takes the date variables, converts them to integer values using the gettime() method (assuming all data is current and not pre 1970) calculates the difference and then records that difference value. That is the time spent between the dates in seconds, which you can then format in any way you like (ex. divide by 60 to get minutes, etc).

 

For reference = JavaScript Dates & ServiceNow - ServiceNow Community & GlideDateTime | ServiceNow Developers

Lastly, have you considered using SLAs to achieve your goal? - you set the start condition to incident creation and the end condition to incident resolution/closure, with "in pending" as the pause condition...and that record will keep track of the duration for you. Then you can report on the average completion percentage of the sla (for all incidents) and multiply that by the duration defined in the sla, to get the average "time worked" on all incidents, and compare that against the actual duration of any given sla record...yea?