- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 12:12 PM - edited 01-10-2024 12:15 PM
The fix script provided below identifies work orders that are in the 'Work In Progress' state but have all their associated tasks closed. This was helpful in pinpointing work orders that are ready for closure.
// Create a GlideRecord query to find work orders in the state of Work In Progress
var workOrderGR = new GlideRecord('wm_order');
workOrderGR.addQuery('state', '=', '18'); // Work In Progress
workOrderGR.query();
// Loop through each work order found
while (workOrderGR.next()) {
var workOrderNumber = workOrderGR.getValue('number'); // Get the work order number
var workOrderSysId = workOrderGR.getValue('sys_id'); // Get the sys_id of the work order
// Create a GlideRecord query to find associated work order tasks
var taskGR = new GlideRecord('wm_task');
taskGR.addQuery('parent', '=', workOrderSysId); // Find tasks associated with the current work order
taskGR.query();
var allTasksClosed = true; // Flag to track if all associated tasks are closed
// Loop through each associated work order task
while (taskGR.next()) {
var taskState = taskGR.getValue('state'); // Get the state of the task
// Check if the task state is not one of the desired states for closure
if (!(taskState == '3' || taskState == '4' || taskState == '7')) {
allTasksClosed = false; // Update flag if any task is not in the desired states for closure
break; // Exit the loop early as we don't need to check other tasks for this work order
}
}
if (allTasksClosed) {
gs.info('Work Order Number ' + workOrderNumber + ' has all associated tasks closed.');
}
}
To monitor these work orders more effectively, I attempted to convert the fix script into a script include. However, when called through a background script, it doesn't yield the same results as the original fix script.
var WorkOrderUtils = Class.create();
WorkOrderUtils.prototype = {
initialize: function() {},
findWorkOrdersInProgressWithClosedTasks: function() {
var workOrdersInProgress = [];
var workOrderGR = new GlideRecord('wm_order');
workOrderGR.addQuery('state', '=', '18'); // Work In Progress
workOrderGR.query();
while (workOrderGR.next()) {
var workOrderNumber = workOrderGR.getValue('number');
var workOrderSysId = workOrderGR.getValue('sys_id');
// Create a GlideRecord query to find associated work order tasks
var taskGR = new GlideRecord('wm_task');
taskGR.addQuery('parent', '=', workOrderSysId);
taskGR.orderByDesc('sys_created_on');
taskGR.query();
var allTasksClosed = true;
while (taskGR.next()) {
var taskState = taskGR.getValue('state');
if (!(taskState == '3' || taskState == '4' || taskState == '7')) {
allTasksClosed = false;
break;
}
}
if (allTasksClosed) {
workOrdersInProgress.push({
number: workOrderNumber,
sys_id: workOrderSysId
});
}
}
return workOrdersInProgress;
},
type: 'WorkOrderUtils'
};
If I manage to rectify the issues with the script include, the subsequent step would involve calling it from a report filter, like this:
Number | is | javascript:new workOrderUtils.findWorkOrdersInProgressWithClosedTasks();
Any help with the script include and calling it from the filter would be greatly appreciated.
Thank you!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 01:10 PM
@Anderson_R Thanks for your validation, I would suggest lets debug this and limit the logic in your script.
First set client callable as true on your script include and then use below script to test.
var WorkOrderUtils = Class.create();
WorkOrderUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
findWorkOrdersInProgressWithClosedTasks: function() {
var workOrdersInProgress = [];
var workOrderGR = new GlideRecord('wm_order');
workOrderGR.addQuery('state', '=', '18'); // Work In Progress
workOrderGR.query();
while (workOrderGR.next()) {
workOrdersInProgress.push(workOrderGR.getValue('number'));
}
return workOrdersInProgress;
},
type: 'WorkOrderUtils'
});
Filter :
Number | is one of | javascript: new workOrderUtils().findWorkOrdersInProgressWithClosedTasks();
Let me know what exactly are you getting after running this filter query.
Thanks,
Harsh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 01:27 PM - edited 01-10-2024 01:30 PM
Script include is set to Client callable.
I used your shortened script and ran a background script to see if it was working and it is. It identifies all Work Orders in the "Work In Progress" state.
However, it doesn't create this list on the report.
Number | is one of | javascript:new workOrderUtils().findWorkOrdersInProgressWithClosedTasks();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 01:33 PM - edited 01-10-2024 01:40 PM
@Anderson_R Alright, lets give another try, would it be possible if you can create new script include , then set client callable as true and use the glide query which i had suggested in my last response.
Sometimes setting existing non client callable SI to client callable SI does not work, that's why i am suggesting to create new one.
Make sure SI prototype has Object.extendsObject(AbstractAjaxProcessor keyword.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 12:34 PM
@Anderson_R Can you please confirm if your script include marked as client callable ? if not please set it and then try with my above solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 09:05 AM
The new script include is marked as client callable.
I got it working for the current script include. To call it in the report filter I had to include the entire API name. Oversight on my part.
javascript:new global.WorkOrderUtils().findWorkOrdersInProgressWithClosedTasks();
Now I just need to finish/correct the script include to complete the other checks.
Thank you for all the help thus far by the way!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 12:06 PM
@Harsh Vardhan
Figured it out! This was the final and correct Script Include:
var WorkOrderUtils = Class.create();
WorkOrderUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
findWorkOrdersInProgressWithClosedTasks: function() {
var workOrdersInProgress = [];
var workOrderGR = new GlideRecord('wm_order');
workOrderGR.addQuery('state', '=', '18');
workOrderGR.query();
while (workOrderGR.next()) {
var workOrderNumber = workOrderGR.getValue('number');
var workOrderSysId = workOrderGR.getValue('sys_id');
var taskGR = new GlideRecord('wm_task');
taskGR.addQuery('parent', '=', workOrderSysId);
taskGR.query();
var allTasksClosed = true;
while (taskGR.next()) {
var taskState = taskGR.getValue('state');
if (!(taskState == '3' || taskState == '4' || taskState == '7')) {
allTasksClosed = false;
break;
}
}
if (allTasksClosed) {
workOrdersInProgress.push(workOrderNumber);
}
}
return workOrdersInProgress;
},
type: 'WorkOrderUtils'
});