- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 07:50 AM
Hi guys,
I need to create a field to calculate an age of incident, but I want to calculate the age with possibilities:
If the resolved field is not empty, so make the calculation between {sys_created_on - resolved_at}
OR
If the resolved field is empty, so make the calculation between {sys_created_on - date now}.
Please, could someone help me with the code to populate my new field?
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 11:09 PM
Hi @MR Carvalho check the below script
var gr = new GlideRecord("incident");
gr.addQuery('number', 'INC0010103'); // Replace with the actual incident number
gr.query();
if (gr.next()) {
var startDateTime = new GlideDateTime(gr.sys_created_on);
if(gr.resolved_at==''){
var endDateTime = new GlideDateTime();
}
else{
var endDateTime = new GlideDateTime(gr.resolved_at);
}
var duration = GlideDateTime.subtract(startDateTime, endDateTime);
var durationInMilliseconds = duration.getNumericValue();
var days = Math.floor(durationInMilliseconds / (1000 * 60 * 60 * 24));
var hours = Math.floor(durationInMilliseconds / (1000 * 60 * 60));
var minutes = Math.floor((durationInMilliseconds % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((durationInMilliseconds % (1000 * 60)) / 1000);
gs.info("Time Difference: " + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 11:09 PM
Hi @MR Carvalho check the below script
var gr = new GlideRecord("incident");
gr.addQuery('number', 'INC0010103'); // Replace with the actual incident number
gr.query();
if (gr.next()) {
var startDateTime = new GlideDateTime(gr.sys_created_on);
if(gr.resolved_at==''){
var endDateTime = new GlideDateTime();
}
else{
var endDateTime = new GlideDateTime(gr.resolved_at);
}
var duration = GlideDateTime.subtract(startDateTime, endDateTime);
var durationInMilliseconds = duration.getNumericValue();
var days = Math.floor(durationInMilliseconds / (1000 * 60 * 60 * 24));
var hours = Math.floor(durationInMilliseconds / (1000 * 60 * 60));
var minutes = Math.floor((durationInMilliseconds % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((durationInMilliseconds % (1000 * 60)) / 1000);
gs.info("Time Difference: " + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2023 06:21 AM
@Eswar Chappa many thanks.