Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Why date and time is getting change using GlideDateTime on Message ?

Virendra K
Kilo Sage

Hi All,

 

I am using dates and time to show it as moratorium conflicts. I am using GlideDateTime but every time when the end date/time is showing wrong on message. (refer snaps and script given below)

What may be the reason? 

How I could set/show the date as it is on message ? I am using below line code, do I need to set the format ?

  output.push(gr1.name + '   ' + stdate.getDisplayValue() + ' to ' + endate.getDisplayValue() + '\n');

 

Its showing correct 1st time,

VirendraK_0-1730908046539.png

 

Just changed the date/time, its picking up the correct slot but Message is showing wrong end date ( see the 1st snap's end date and below end date(highlighted)) 

VirendraK_1-1730908274183.png

 

SI Script :

var checkMoratoriumDates = Class.create();
checkMoratoriumDates.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getallValues: function() {
        var output = [];

        var xplannedStart = new GlideDateTime(this.getParameter("sysparm_startDt")); // Planned start date
        var xplannedStartDate= xplannedStart.getDate();
        var xplannedEnd = new GlideDateTime(this.getParameter("sysparm_endDt")); // Planned end date
        var xplannedEndDate = xplannedEnd.getDate();

        gs.log('Vir-TCS: Start and End dt 1st= ' + xplannedStartDate + " to " + xplannedEndDate);
        // plannedStart.setDisplayValue(plannedStart.getDisplayValue().split(" ")[0] + " 00:00:00");
        // plannedEnd.setDisplayValue(plannedEnd.getDisplayValue().split(" ")[0] + " 23:59:59");
        var plannedStart = new GlideDateTime(xplannedStartDate + " 12:00:00");
        var plannedEnd = new GlideDateTime(xplannedEndDate + " 23:59:00");

        var timeZone = this.getParameter("sysparm_timeZone");
        var divisionVal = this.getParameter("sysparm_division");

        gs.log('Vir-TCS: Start and End dt = ' + plannedStart + " to " + plannedEnd);

        var cnt = 1;
        var gr = new GlideRecord('cmn_schedule_blackout');
        var query1 = "type=blackout"; //^condition=u_division=" + divisionVal + "^^EQ";
        //gs.log('Vir-TCS : Q1 = ' + query1);
        gr.addEncodedQuery(query1);
        gr.query();
        while (gr.next()) {
            var gr1 = new GlideRecord('cmn_schedule_span');
            //var query2 = "schedule.type=blackout^schedule.name=" + gr.name + "^u_start_time>=javascript&colon;gs.dateGenerate('" + startDateStr[0] + "','" + startTimeStr[1] + "')^u_end_time<=javascript&colon;gs.dateGenerate('" + endDateStr[0] + "','" + endTimeStr[1] + "')";
            var query2 = "schedule.type=blackout^schedule.name=" + gr.name; //+ "^u_start_time>=javascript&colon;gs.dateGenerate('" + startDateStr[0] + "','00:00:01')^u_end_time<=javascript&colon;gs.dateGenerate('" + endDateStr[0] + "','23:59:59')";

            gr1.addEncodedQuery(query2);
            gs.log('Vir-TCS : Q2 = ' + query2);
            gr1.query();
            var stdate;
            var endate;

            while (gr1.next()) {
                stdate = new GlideDateTime(gr1.getValue("u_start_time")); // Convert start time to GlideDateTime
                endate = new GlideDateTime(gr1.getValue("u_end_time")); // Convert end time to GlideDateTime
               
                gs.log("Vijeta: Record Start Date = " + stdate.getDisplayValue() + " | End Date = " + endate.getDisplayValue());
                gs.log("Vir-TCS Count " + gr1.getRowCount());
                gs.log("Vir-TCS Passed");

                // Check if the record falls within the planned range
                if (stdate <= plannedEnd && endate >= plannedStart) {
                    if (cnt == 1) {
                        output.push('<b>MORATORIUM CONFLICTS</b><br/>');
                        cnt = cnt + 1;
                    }
                    gs.log("VIREN: Passed - Record within date range.");
                    output.push(gr1.name + '   ' + stdate.getDisplayValue() + ' to ' + endate.getDisplayValue() + '\n');
                }

            }
        }

        return output.join('<br/>');

    },
    type: 'checkMoratoriumDates '
});

 

Thanks,

Virendra

 

 

 

 

0 REPLIES 0