Anyone knows of a way to mass close work orders? A...
# administration
j
Anyone knows of a way to mass close work orders? And no, is not a checkbox at the line level or a dropdown on the WO. It's a button
c
I think you'll need a SuiteScript solution for this. Create a mass update script that searches your work orders and clicks the "Close" button using nlapiSubmitField or record.submitFields, depending on your SuiteScript version.
l
If Iโ€™m not wrong, the closed field is also present on Work Order transactions so if you update this field via script to Yes on WO lines, it will change WO status to closed
j
@Luiz Morais I though that at first but no close field at the line level.
l
sorry I forgot to mention this is a hidden field : try this one, it will change the status to Closed:
Copy code
require(['N/record','N/search', 'N/format'], function(record, search, format){
var wo = record.load({type: 'workorder', id: 75473579});

for(var i = 0; i < wo.getLineCount('item'); i++)
{
wo.setSublistValue('item', 'isclosed', i, true);
}
wo.save();

})
๐Ÿ˜ฏ 1
j
@Luiz Morais This worked perfect!! Thank you
Let me know if you have a buy me a coffee or something similar. I appreciate the help
l
Appreciated but itโ€™s not needed. ๐Ÿ™‚ Thanks
j
I modified to run off of a saved search and passing the favor
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 * @NAuthor J.C. 
 */
define(['N/record', 'N/search', 'N/log'], function(record, search, log) {
    function execute(context) {
        // Replace 'saved_search_id' with the actual ID of your saved search
        var savedSearchId = 'customsearchol_open_work_orders_3_4_2'; // Update with your saved search ID
        var workOrderSearch = search.load({
            id: savedSearchId
        });

        // Iterate through the search results
        workOrderSearch.run().each(function(result) {
            try {
                // Get the internal ID of the work order from the search result
                var workOrderId = result.id;

                // Load the work order record
                var wo = record.load({
                    type: 'workorder',
                    id: workOrderId,
                    isDynamic: false // Set to true if you prefer dynamic mode
                });

                // Get the line count of the 'item' sublist and set 'isclosed' to true
                var lineCount = wo.getLineCount({ sublistId: 'item' });
                for (var i = 0; i < lineCount; i++) {
                    wo.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'isclosed',
                        line: i,
                        value: true
                    });
                }

                // Save the work order
                wo.save({
                    ignoreMandatoryFields: false // Set to true if you want to bypass mandatory field validation
                });

                log.debug({
                    title: 'Work Order Updated',
                    details: 'Work Order ID: ' + workOrderId + ' updated successfully.'
                });
            } catch (e) {
                log.error({
                    title: 'Error Processing Work Order',
                    details: 'Work Order ID: ' + workOrderId + ', Error: ' + e.message
                });
            }

            // Continue processing the next result (return true to continue)
            return true;
        });
    }

    return {
        execute: execute
    };
});
๐Ÿ‘ 1
a
You can also accomplish this with a Saved Search and a scheduled WF.