Update Item status to inactive

Requirement

We need yo update an to inactive item records if the following conditions are satisfied:

  1. Item is discontinued item
  2. Total inventory is zero (Quantity on hand in item record is zero)
  3. If there is no purchase orders exist for the item in the following status
    1. Purchase order: Pending Supervisor approval 
    2. Purchase order: Partially received 
    3. Purchase order: Pending billing/Partially Received 
    4. Purchase order: Pending Receipt 
  4. “Quantity in transit(external)” in the item record is blank.

Solution

We can create a scheduled script to change the item status when above conditions are met.

/**
 *@NApiVersion 2.x
 *@NScriptType ScheduledScript
 *@NModuleScope SameAccount
 */


define(['N/search', 'N/record', 'N/https', 'N/runtime', 'N/config'], function (search, record, https, runtime, config) {
    var searchfunc = {
        iterateSavedSearch: function (searchObj) { //to iterate over and initiate format of each saved search result 
            var response = [];
            var searchPageRanges;
            try {
                searchPageRanges = searchObj.runPaged({
                    pageSize: 1000
                });
            } catch (err) {
                return [];
            }
            if (searchPageRanges.pageRanges.length < 1)
                return [];

            var pageRangeLength = searchPageRanges.pageRanges.length;
            log.debug('pageRangeLength', pageRangeLength);

            for (var pageIndex = 0; pageIndex < pageRangeLength; pageIndex++)
                searchPageRanges.fetch({
                    index: pageIndex
                }).data.forEach(function (result) {
                    response.push(result);
                });

            return response;
        }
    };
    var main = {
        itemSearchResult: function () {
            try {
                var itemSearchResult = search.create({
                    type: "inventoryitem",
                    filters:
                        [
                            ["type", "anyof", "InvtPart"],
                            "AND",
                           ["isinactive", "is", "F"],
                           "AND",
                            ["custitem_discontinued", "is", "T"],
                            "AND",
                            [["quantityonhand", "equalto", "0"], "OR", ["quantityonhand", "isempty", ""]],
                            "AND",
                            ["parent", "noneof", "@NONE@"],
                            "AND",
                            ["transaction.type","anyof","PurchOrd"],
                            "AND",
                            ["transaction.status","noneof","PurchOrd:A","PurchOrd:B","PurchOrd:D","PurchOrd:E","PurchOrd:F"],
                            "AND",
                            ["locationqtyintransitext","isempty",""],

                        ],
                    columns:
                        [
                            search.createColumn({ name: "itemid", label: "Name" }),
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                });
                var searchResultCount = itemSearchResult.runPaged().count;
                 log.debug("itemSearchResult count", searchResultCount);
                return searchfunc.iterateSavedSearch(itemSearchResult);
            } catch (e) {
                log.debug('error@itemSearchResult', e)
            }
        },

        execute: function (context) {
            try {
                var currentScript = runtime.getCurrentScript();
                var itemsearchobj = main.itemSearchResult();
                for (var key in itemsearchobj) {
                    if (Number(currentScript.getRemainingUsage()) < 250) {
                        return false;
                    }
                     log.debug('itemsearchobj[key]', itemsearchobj[key])
                    //return true;
                    var makeInactive = record.submitFields({
                        type: record.Type.INVENTORY_ITEM,
                        id: itemsearchobj[key].id,
                        values: {
                            isinactive: true,
                            custitem_sync_to_wholesale: false,
                            custitem_sync_to_retail: false,
                            isonline: false
                        },
                        options: {
                            enableSourcing: false,
                            ignoreMandatoryFields: true
                        }
                    });
                }
            } catch (e) {
                log.debug('error@execute', e)
            }
        }
    }
    return main;
});

Leave a comment

Your email address will not be published. Required fields are marked *