- 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 08:07 AM
Hi @MR Carvalho can you please help me with approach, How you want to calculate the age for reference find below community article:
https://www.servicenow.com/community/developer-forum/how-to-calculate-age-of-an-incident/m-p/1424075
Please, Mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Best Regards,
Eswar Chappa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 08:07 AM
var resolvedAt = current.resolved_at;
var createdOn = current.sys_created_on;
if (resolvedAt) {
var ageInMillis = resolvedAt - createdOn;
} else {
var currentDate = new GlideDateTime();
var ageInMillis = currentDate.getNumericValue() - createdOn;
}
var ageInDays = ageInMillis / (1000 * 60 * 60 * 24);
current.age_of_incident = ageInDays;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 08:33 AM
hi @Harish Bainsla I tried to use your code, but I dont know why is not working. Please, can you check whats is wrong? Below is the code.
var gr = new GlideRecord("incident");
gr.addQuery('number', 'INC0183425'); //try pasing any random incident number for check
gr.query();
while (gr.next()) {
var resolvedAt = gr.resolved_at;
var createdOn = gr.sys_created_on;
if (resolvedAt) {
var ageInMillis = resolvedAt - createdOn;
}
else {
var currentDate = new GlideDateTime();
var ageInMillis = currentDate.getNumericValue() - createdOn;
}
var ageInDays = ageInMillis / (1000 * 60 * 60 * 24);
gr.age_of_incident = ageInDays;
gs.print(gr);
}
Many thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 09:09 PM
var gr = new GlideRecord("incident");
gr.addQuery('number', 'INC0183425'); // Replace with the actual incident number you want to check
gr.query();
while (gr.next()) {
var resolvedAt = gr.resolved_at;
var createdOn = gr.sys_created_on;
if (resolvedAt) {
var ageInMillis = new GlideDateTime(resolvedAt).getNumericValue() - new GlideDateTime(createdOn).getNumericValue();
} else {
var currentDate = new GlideDateTime();
var ageInMillis = currentDate.getNumericValue() - new GlideDateTime(createdOn).getNumericValue();
}
var ageInDays = ageInMillis / (1000 * 60 * 60 * 24);
gr.age_of_incident = ageInDays;
gr.update(); // Save the updated value back to the record
gs.print(gr);
}