- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-09-2024 08:03 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-10-2024 12:50 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-10-2024 12:50 AM
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.