Inquiry about Servicenow Report calculation field

ab_frostbite
Tera Contributor

Do we have a field that calculate the time difference between two fields. Can you please explain. Thanks in advance 

3 REPLIES 3

Shivalika
Mega Sage

Hello @ab_frostbite 

 

Just create a custom field of string type and use below šŸ‘‡ script : 

 

 

var gr = new GlideRecord('incident');

if (gr.get('<sys_id>')) { // Replace with an actual sys_id or query condition

    var opened = new GlideDateTime(gr.opened_at);

    var closed = new GlideDateTime(gr.closed_at);

 

    var dur = GlideDateTime.subtract(closed, opened);

 

    gs.info(dur.getDisplayValue

());

 

Populate that either using a scheduled job or business rule based on your conditions..

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

}

Robert H
Mega Sage

Hello @ab_frostbite ,

 

I assume you already have a report on Change Request and just need advice on how to add that calculated field.

 

In your Report creation screen ("Configure" step) please follow the steps shown in this screen shot:

RobertH_0-1743624093768.png

The function to use is:

glidefunction:datediff(closed_at,end_date)

 

Now save the report, click on "Choose columns" and your new calculated field will be available to be added.

 

Result:

RobertH_1-1743624282876.png

 

Regards,

Robert

 

 

Rohit  Singh
Mega Sage

Hi @ab_frostbite ,

 

You create one custom field of type date and time. Then create a before BR on update on the table which have these fields. Condition Planned Start Date and Closed Date is not empty.

 

Use belo code

 

Current.custom_field_name = current.closed_date - current.planned_date

 

modify the field name as per the exact field name.

 

If my response helped, then please accept the solution and hit thumbs up.

 

Regards,

Rohit