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 ,

Please try with the script below and let me know how it works for you.


Background Script:

var todayDate = new GlideDateTime();
var latestDate = todayDate.getDisplayValueInternal();
var totalRecordsWithLatestDate = 0;
var continueProcessing = true;

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

    while (gr.next()) {
        var recordDate = gr.getDisplayValueInternal('depreciation_date');
        if (recordDate == latestDate) {
            totalRecordsWithLatestDate++;
        }
    }

    if (totalRecordsWithLatestDate >= 2) {
        var nextDay = new GlideDateTime(latestDate);
        nextDay.addDays(1);
        latestDate = nextDay.getDisplayValueInternal();
        todayDate.addDays(1);
        totalRecordsWithLatestDate = 0;
    } else {
        gs.print("Latest Date: " + latestDate);
        continueProcessing = false;
    }
}

 

Script Include:

var aggregateUtils = Class.create();
aggregateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    findDateWithLessRecords: function() {
        var todayDate = new GlideDateTime();
        var latestDate = todayDate.getDisplayValueInternal();
        var totalRecordsWithLatestDate = 0;
        var continueProcessing = true;

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

            while (gr.next()) {
                var recordDate = gr.getDisplayValueInternal('depreciation_date');
                if (recordDate == latestDate) {
                    totalRecordsWithLatestDate++;
                }
            }

            if (totalRecordsWithLatestDate >= 2) {
                var nextDay = new GlideDateTime(latestDate);
                nextDay.addDays(1);
                latestDate = nextDay.getDisplayValueInternal();
                todayDate.addDays(1);
                totalRecordsWithLatestDate = 0;
                return latestDate;
            } else {
                continueProcessing = false;
                return latestDate;
            }
        }
    },
    type: 'aggregateUtils'
});

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,
Aniket

 

Hi Aniket,

Thank for response.

 

Requirement is 

ROS4349_1-1705067407430.png

 

above date should be populated if alm_hardware table has 

alm_hardware table has depreciation_date field.

if 2 records has depreciation_date with current date, in above screen shot there is a date time type variable should populate tomorrow date.

If with tomorrow date also we have 2 records it should show day after tomorrow date.

 

basically it should run a iteration alm_hardware table will be checked with depreciation_date field if we have 2 records with same date it should skip that date and check for next available day which date has less that 2 records and populate that date in above shown date time type variable.

 

and loop should run till it reached warrenty expiration date.

Hello @ROS4349 ,

Please give a try to the modified script below:


Background Script:

var todayDate = new GlideDateTime();
var warrantyExpirationDate = gs.dateGenerate(todayDate.getYear(), todayDate.getMonth(), todayDate.getDayOfMonth() + 365); // Assuming 1-year warranty
var currentDate = todayDate.getDisplayValueInternal();

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

    var totalRecordsWithCurrentDate = gr.getRowCount();

    if (totalRecordsWithCurrentDate < 2) {
        gs.print("Selected Date: " + currentDate);
        break;
    }

    // Move to the next day
    var nextDay = new GlideDateTime(currentDate);
    nextDay.addDays(1);
    currentDate = nextDay.getDisplayValueInternal();
}

// Additional logic as needed

 

Script Include:

var aggregateUtils = Class.create();
aggregateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    findDateWithLessRecords: function() {
        var todayDate = new GlideDateTime();
        var warrantyExpirationDate = gs.dateGenerate(todayDate.getYear(), todayDate.getMonth(), todayDate.getDayOfMonth() + 365); // Assuming 1-year warranty
        var currentDate = todayDate.getDisplayValueInternal();

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

            var totalRecordsWithCurrentDate = gr.getRowCount();

            if (totalRecordsWithCurrentDate < 2) {
                return currentDate;
            }

            // Move to the next day
            var nextDay = new GlideDateTime(currentDate);
            nextDay.addDays(1);
            currentDate = nextDay.getDisplayValueInternal();
        }

        // Return null or handle the case where no suitable date is found
        return null;
    },
    type: 'aggregateUtils'
});

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,
Aniket

 

Madhukar Donepu
Tera Contributor

Hello @ROS4349 ,

 

Try the following code and mark helpful , if this code works for you:

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

while (continueProcessing) {
var gr = new GlideRecord('alm_hardware');
gr.addQuery('depreciation_date', '>=', latestDate);
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++;
}
}

if (totalRecordsWithLatestDate >= 2) {
var nextDay = new GlideDateTime(latestDate);
nextDay.addDays(1);
latestDate = nextDay.getDisplayValue().split(' ')[0];
todayDate.addDays(1);
totalRecordsWithLatestDate = 0;
} else {
continueProcessing = false;
return latestDate;

}
}