Displaying exact date from the cmdb_ci record creation to current date when the record is loaded
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 01:03 PM
When a user opens a CI record, an informational message should display the age of the record from its creation date in the format "Years Months Days Hours Minutes".
For this I tried using script include and client script from a scoped application to test according to my requirement but its not working. I'm new to servicenow and I don't have much knowledge about the GlideDateTime
Here is my script include code:
GetCIDateDetails = Class.create();
GetCIDateDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
and here is my client script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 07:03 PM
Hi @michaelk8405720,
Try the following, note that this is not perfect as we are assuming each month contain 31 days. You might want to test this with different use cases.
On a side note, I think you can also use a Business Rule (Display) instead of writing a Script Include and a Client Script.
GetCIDateDetails = Class.create();
GetCIDateDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getCIDateDetails: function() {
var cmdbSysId = this.getParameter('sysparm_ci_sys_id');
if (!cmdbSysId) return "Invalid Configuration item";
var ciGr = new GlideRecord('cmdb_ci');
if (!ciGr.get(cmdbSysId)) return "CI not found";
var now = new GlideDateTime();
var timeDiff = GlideDateTime.subtract(createdDate, now);
var daysDiff = timeDiff.getDayPart();
var years = Math.floor(daysDiff / 365);
var months = Math.floor((daysDiff - (years * 365)) / 31);
var days = daysDiff - (years * 365) - (months * 31);
var hours = timeDiff.getByFormat('HH');
var minutes = timeDiff.getByFormat('mm');
return years + " Years " + months + " Months " + days + " Days " + hours + " Hours " + minutes + " Minutes";
},
type: 'GetCIDateDetails'
}
Cheers