Displaying exact date from the cmdb_ci record creation to current date when the record is loaded

michaelk8405720
Tera Contributor

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);
        }
    });
}

 

1 REPLY 1

James Chun
Kilo Patron

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