Days Since Last Major Incident

dana_montei
Tera Contributor

Trying this code below to generate the days since last major incident. I have the latest being 17 days ago but this generates the following "20,263 Days":

dana_montei_0-1750774771100.png

Any thoughts on correcting this?

1 ACCEPTED SOLUTION

After adding some log statements I saw that the script was returning nothing for opened_at. Then I realized you were missing a peace of code. Here is the updated script for that section.

var grIncident = new GlideRecord('incident');
    grIncident.addQuery('major_incident_state', 'accepted');
    grIncident.orderByDesc('opened_at');
    grIncident.setLimit(1);
    grIncident.query();
    if (grIncident.next()) { //Missing this if statment which tells the system which record to look at to get the data.
        var createdDateValue = grIncident.getDisplayValue('opened_at');
        // gs.log("Created Date: " + createdDateValue);
        var createdDate = new GlideDateTime(createdDateValue);
        var currentDate = new GlideDateTime().getLocalDate();
        var createdTimeMs = createdDate.getNumericValue();
        // gs.log("CreatedTime " + createdTimeMs);
        var currentTimeMs = currentDate.getNumericValue();
        // gs.log("currentTime " + currentTimeMs);
        var timeDiffMs = currentTimeMs - createdTimeMs;
        var openDays = Math.floor(timeDiffMs / 86400000);
        retun openDays + " Days";
        // gs.log(openDays + "   Days");
    }

View solution in original post

4 REPLIES 4

Brian Lancaster
Tera Sage

Change this line var createdDateValue = grIncident.getValue('opened_at') to var createdDateValue = grIncident.getDisaplyValue('opened_at').

I believe the issue is that getValue will return the backend value of the date/time field which is stored in GMT. Where getDisplayValue will return what is displayed to the user which would be local time.

 

PS. in the future please use the insert edit code function in the HTML editor of the question you are posting to past the code instead of providing a screenshot. It will make it easier to test your code in our PDI.

BrianLancaster_0-1750777090493.png

 

Thanks, still getting the same inaccurate days display. Here is the code:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g2:evaluate var="jvar_changes" object="true">
		//<![CDATA[
			(function daysSinceMajor() {
				var grIncident = new GlideRecord('incident');
				grIncident.addQuery('major_incident_state', 'accepted');
				grIncident.orderByDesc('opened_at');
				grIncident.setLimit(1);
				grIncident.query();
				
				var createdDateValue = grIncident.getDisplayValue('opened_at');
				var createdDate = new GlideDateTime(createdDateValue);
				var currentDate = new GlideDateTime().getLocalDate();
				var createdTimeMs = createdDate.getNumericValue();
				var currentTimeMs = currentDate.getNumericValue();
				var timeDiffMs = currentTimeMs - createdTimeMs;
				var openDays = Math.floor(timeDiffMs / 86400000);
				return openDays + "   Days";
			})();
		//]]>
	</g2:evaluate>
	<h1 style="text-align:center;">$[jvar_changes]</h1>
</j:jelly>

After adding some log statements I saw that the script was returning nothing for opened_at. Then I realized you were missing a peace of code. Here is the updated script for that section.

var grIncident = new GlideRecord('incident');
    grIncident.addQuery('major_incident_state', 'accepted');
    grIncident.orderByDesc('opened_at');
    grIncident.setLimit(1);
    grIncident.query();
    if (grIncident.next()) { //Missing this if statment which tells the system which record to look at to get the data.
        var createdDateValue = grIncident.getDisplayValue('opened_at');
        // gs.log("Created Date: " + createdDateValue);
        var createdDate = new GlideDateTime(createdDateValue);
        var currentDate = new GlideDateTime().getLocalDate();
        var createdTimeMs = createdDate.getNumericValue();
        // gs.log("CreatedTime " + createdTimeMs);
        var currentTimeMs = currentDate.getNumericValue();
        // gs.log("currentTime " + currentTimeMs);
        var timeDiffMs = currentTimeMs - createdTimeMs;
        var openDays = Math.floor(timeDiffMs / 86400000);
        retun openDays + " Days";
        // gs.log(openDays + "   Days");
    }

This worked perfect, added in the code line you provided and it is now representing the correct day count.