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

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. 

Anderson_R_0-1704921888123.png


However, it doesn't create this list on the report.

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

Anderson_R_1-1704921981210.png

 



@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. 

 

@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. 

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. 

Anderson_R_0-1704991757630.png

 

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! 



@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'
});