Script hangs when fetching in-progress HR Cases without a limit.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2025 11:48 PM
Hi Community,
We have a requirement to retrieve a large number of in-progress HR Life Cycle Event records where all associated HR tasks, requests, etc., are closed. We created a script include to handle this, but it works only when we set a limit of 15,000 records. If we remove the limit, the script runs indefinitely. When calling this script include in a report, it causes the report to load continuously and eventually makes the screen unresponsive.
Below is the script include for reference. Could you please suggest improvements or an optimized approach?
var gr = new GlideRecord('sn_hr_le_case');
gr.addActiveQuery();
gr.setLimit(15000);
gr.query();
var filteredArr = []; // Array to store sys_ids
while (gr.next()) {
var hasActiveTask = false; // Flag for active HR tasks
var hasActiveRequest = false; // Flag for active SC requests
var hasActiveGroupOp = false; // Flag for active group operations
// Check for active HR tasks
var taskGr = new GlideRecord('sn_hr_core_task');
taskGr.addQuery('parent', gr.getValue('sys_id'));
taskGr.addActiveQuery();
taskGr.setLimit(1);
taskGr.query();
if (taskGr.hasNext()) {
hasActiveTask = true;
}
// Check for active SC requests
var reqGr = new GlideRecord('sc_request');
reqGr.addQuery('active', true);
reqGr.addQuery('parent', gr.getValue('sys_id'));
reqGr.setLimit(1);
reqGr.query();
if (reqGr.hasNext()) {
hasActiveRequest = true;
}
// Check for active Group Operations
var grpOpGr = new GlideRecord('some table);
grpOpGr.addQuery('active', true);
grpOpGr.addQuery('parent', gr.getValue('sys_id')); // Ensure Group Operation is linked to HR case
grpOpGr.setLimit(1);
grpOpGr.query();
if (grpOpGr.hasNext()) {
hasActiveGroupOp = true;
}
// Add HR case to array if it has NO active tasks, NO active requests, and NO active group operations
if (!hasActiveTask && !hasActiveRequest && !hasActiveGroupOp) {
filteredArr.push(gr.getValue('sys_id').trim());
}
}
return filteredArr;
0 REPLIES 0