my script is returning 15-01-2024 in background script and the same code is returning 13-01-2024

ROS4349
Tera Expert

my script is returning 15-01-2024 in my background script and the same code is returning 13-01-2024 in script include which I need to use for catalog item.

 

Background script: 

 

var todayDate = new GlideDateTime();
var localDate = todayDate.getDisplayValue();
var latestDate = localDate.split(' ')[0];
var totalRecordsWithLatestDate = 0;
var continueProcessing = true;

gs.log("today's date - "+todayDate);

gs.log("local date -"+localDate);

gs.log("latest date - "+latestDate);

while (continueProcessing) {
var gr = new GlideRecord('alm_hardware');
gr.addQuery('depreciation_date', '>=', latestDate + ' 00:00:00');
gr.addQuery('depreciation_date', '<=', latestDate + ' 23:59:59');
gr.query();

while (gr.next()) {
var recordDate = gr.getDisplayValue('depreciation_date').split(' ')[0];

if (recordDate == latestDate) {
totalRecordsWithLatestDate++;
}
}

gs.print("Total Records with Latest Date - " + totalRecordsWithLatestDate);

if (totalRecordsWithLatestDate >= 2) {
var nextDay = new GlideDateTime(latestDate);
var hours = 60 * 60 * 30;
nextDay.addSeconds(hours);
latestDate = nextDay.getDate().getDisplayValue();
todayDate.addSeconds(hours);
gs.print("Moving to Next Day: " + latestDate);
totalRecordsWithLatestDate = 0; // Reset the counter for the new date
} else {
gs.print("Latest Date: " + latestDate);
continueProcessing = false; // Set the flag to exit the loop
}
}

-------------------------------------------------------------------------------

Output:

ROS4349_0-1705042971599.png

===============================================================

==========================================================

Script include:

 

var aggregateUtils = Class.create();
aggregateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    findDateWithLessRecords: function() {
        var todayDate = new GlideDateTime();
        var localDate = todayDate.getDisplayValue();
        var latestDate = localDate.split(' ')[0];
        var totalRecordsWithLatestDate = 0;
        var continueProcessing = true;
        // gs.log("today's date - "+todayDate);
        // gs.log("local date -"+localDate);
        // gs.log("latest date - "+latestDate);
        while (continueProcessing) {
           var gr = new GlideRecord('alm_hardware');
            gr.addQuery('depreciation_date', '>=', latestDate + ' 00:00:00');
            gr.addQuery('depreciation_date', '<=', latestDate + ' 23:59:59');
            gr.query();
            while (gr.next()) {
                var recordDate = gr.getDisplayValue('depreciation_date').split(' ')[0];
                if (recordDate == latestDate) {
                    totalRecordsWithLatestDate++;
                }
            }
            //gs.print("Total Records with Latest Date - " + totalRecordsWithLatestDate);
            if (totalRecordsWithLatestDate >= 2) {
                var nextDay = new GlideDateTime(latestDate);
                var hours = 60 * 60 * 24;
                nextDay.addSeconds(hours);
                latestDate = nextDay.getDate().getDisplayValue();
                todayDate.addSeconds(hours);

                //gs.print("Moving to Next Day: " + latestDate);
                totalRecordsWithLatestDate = 0; // Reset the counter for the new date               
                return latestDate;
            } else {
                //gs.print("Latest Date: " + latestDate);
                continueProcessing = false; // Set the flag to exit the loop
           
                return latestDate;

            }
        }
    },
    type: 'aggregateUtils'
});

==================================================

Client Script:

 

function onLoad() {

    var aggregateUtils = new GlideAjax('aggregateUtils');
    aggregateUtils.addParam('sysparm_name', 'findDateWithLessRecords');
    aggregateUtils.getXML(DateDetails);

    function DateDetails(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer) {
            //alert(answer);
            g_form.setValue('schedule_the_date_of_your_asset_refresh', answer);
            //next_slot_available_date

        } else {
            //alert('No date found with less than 10 records.');
        }
    }

}

---------------------------------------------------------------------------------

Output:

ROS4349_1-1705043069858.png

 

 

Attaching Bachground script output and script include output. Pls help me with the fixing.

1 ACCEPTED SOLUTION

Aniket Chavan
Tera Sage
Tera Sage

Hello @ROS4349 

I wanted to check in regarding the response I provided. If my suggestions were beneficial in addressing your query or helped in resolving your issue, would you mind marking it as helpful, accepting the solution, and closing the thread? Your acknowledgment not only shows appreciation for the assistance but also assists future readers who might come across a similar problem.

Thank you for your consideration!

Best regards,
Aniket.

View solution in original post

6 REPLIES 6

Aniket Chavan
Tera Sage
Tera Sage

Hello @ROS4349 

I wanted to check in regarding the response I provided. If my suggestions were beneficial in addressing your query or helped in resolving your issue, would you mind marking it as helpful, accepting the solution, and closing the thread? Your acknowledgment not only shows appreciation for the assistance but also assists future readers who might come across a similar problem.

Thank you for your consideration!

Best regards,
Aniket.

Hi Aniketha,

Your code is not working on Date,time field so created a date type field and customized.

Anyways I got help from your code. Thank you...