Converting Fix Script to Script Include | Calling Script Include in Report Filter

Anderson_R
Tera Contributor

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();

Anderson_R_0-1704917468252.png


Any help with the script include and calling it from the filter would be greatly appreciated. 

Thank you! 




1 ACCEPTED SOLUTION

@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

View solution in original post

9 REPLIES 9

Harsh Vardhan
Giga Patron

Hi @Anderson_R  Can you try to push only number value in array and then use below filter in your list. 

 

Number | is One Of | javascript: new workOrderUtils().findWorkOrdersInProgressWithClosedTasks();

 

Thanks,

Harsh

 

 

Number | is one of | javascript: new workOrderUtils().findWorkOrdersInProgressWithClosedTasks();

 

 

Note: looks like text editor converting : to : , so please check script syntax before running it. 

Hi @Harsh Vardhan

 

I updated the array so that it only pushes the number value and changed the filter to is one of. But no luck. I think something is incorrect with the script include because I'm running the following background script:

// Instantiate the Script Include
var workOrderUtils = new WorkOrderUtils();

// Call the function
var identifiedWorkOrders = workOrderUtils.findWorkOrdersInProgressWithClosedTasks();

// Log the result to the background script's log
gs.info('Identified Work Orders In Progress with Closed Tasks: ' + identifiedWorkOrders);

But only get these results: 

Anderson_R_0-1704920256047.png

 



@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