Script to count RITM

tsoct
Tera Guru

Hello, everyone.
I need to count the number of ritms every day depending on the actual end and actual start field.


If:

1) RITM123456's Actual Start is July 9, 2024, and its Actual End is July 11, 2024,

2) RITM789101's Actual Start is July 9, 2024, and its Actual End is July 9, 2024,

the script count should return:
09 July count = 2.
10 July count = 1.
11 July count = 1.

How can this be accomplished using script?

1 ACCEPTED SOLUTION

Hi @tsoct,

please check below script:

var gr = new GlideRecord('sc_req_item');
gr.addActiveQuery();
gr.query();

var dateCounts = {};

while (gr.next()) {
    var startDate = new GlideDateTime(gr.getValue('actual_start')).getLocalDate().getDisplayValue();
    var endDate = new GlideDateTime(gr.getValue('actual_end')).getLocalDate().getDisplayValue();
    var quantity = parseInt(gr.getValue('quantity'));

    var currentDate = new GlideDateTime(startDate);
    var endDateTime = new GlideDateTime(endDate);

    while (currentDate <= endDateTime) {
        var dateStr = currentDate.getLocalDate().getDisplayValue();

        if (!dateCounts[dateStr]) {
            dateCounts[dateStr] = { count: 0, quantity: 0 };
        }

        dateCounts[dateStr].count += 1;
        dateCounts[dateStr].quantity += quantity;

        currentDate.addDaysLocalTime(1);
    }
}

for (var date in dateCounts) {
    gs.print(date + ' count = ' + dateCounts[date].count + '. Quantity = ' + dateCounts[date].quantity);
}

Thank you, please make helpful if you accept the solution.

View solution in original post

5 REPLIES 5

Hi @tsoct,

please check below script:

var gr = new GlideRecord('sc_req_item');
gr.addActiveQuery();
gr.query();

var dateCounts = {};

while (gr.next()) {
    var startDate = new GlideDateTime(gr.getValue('actual_start')).getLocalDate().getDisplayValue();
    var endDate = new GlideDateTime(gr.getValue('actual_end')).getLocalDate().getDisplayValue();
    var quantity = parseInt(gr.getValue('quantity'));

    var currentDate = new GlideDateTime(startDate);
    var endDateTime = new GlideDateTime(endDate);

    while (currentDate <= endDateTime) {
        var dateStr = currentDate.getLocalDate().getDisplayValue();

        if (!dateCounts[dateStr]) {
            dateCounts[dateStr] = { count: 0, quantity: 0 };
        }

        dateCounts[dateStr].count += 1;
        dateCounts[dateStr].quantity += quantity;

        currentDate.addDaysLocalTime(1);
    }
}

for (var date in dateCounts) {
    gs.print(date + ' count = ' + dateCounts[date].count + '. Quantity = ' + dateCounts[date].quantity);
}

Thank you, please make helpful if you accept the solution.