Displaying exact date from the cmdb_ci record creation to current date when the record is loaded
Options
- 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, {
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 createdDate = new GlideDateTime(ciGr.getValue('sys_created_on'));
var now = new GlideDateTime();
var createdJS = new Date(createdDate.getDisplayValue());
var nowJS = new Date(now.getDisplayValue());
var years = nowJS.getFullYear() - createdJS.getFullYear();
var months = nowJS.getMonth() - createdJS.getMonth();
var days = nowJS.getDate() - createdJS.getDate();
var hours = nowJS.getHours() - createdJS.getHours();
var minutes = nowJS.getMinutes() - createdJS.getMinutes();
if (minutes < 0) { minutes += 60; hours--; }
if (hours < 0) { hours += 24; days--; }
if (days < 0) {
var prevMonth = new Date(nowJS.getFullYear(), nowJS.getMonth(), 0);
days += prevMonth.getDate();
months--;
}
if (months < 0) { months += 12; years--; }
return years + " Years " + months + " Months " + days + " Days " + hours + " Hours " + minutes + " Minutes";
},
type: 'GetCIDateDetails'
}
and here is my client script:
function onLoad() {
var ga = new GlideAjax('GetCIDateDetails');
ga.addParam('sysparm_name', 'getCIDateDetails');
ga.addParam('sysparm_ci_sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(function(response) {
if (response) {
g_form.addInfoMessage("Configuration Item Age is " + response);
}
});
}