Inventory Count

Note:

This topic applies to all versions of SuiteScript.

The inventory count record enables you to maintain better inventory accuracy and tighter control of assets.

The inventory count record is available only when the Inventory Count feature is enabled at Setup > Company > Enable Features, on the Items & Inventory subtab.

In the UI, you access the inventory count record at Transactions > Inventory > Enter Inventory Count.

For help working with this record in the UI, see Inventory Count.

The internal ID for this record is inventorycount.

See the SuiteScript Records Browser for all internal IDs associated with this record. For information about using the SuiteScript Records Browser, see Working with the SuiteScript Records Browser in the NetSuite Help Center.

For information about scripting with this record in SuiteScript, see the following help topics:

Supported Script Types

The inventory count record is scriptable in both client and server SuiteScript.

All three user events are supported: beforeLoad, beforeSubmit, and afterSubmit.

Supported Functions

The inventory count record is fully scriptable — it can be created, updated, copied, deleted, and searched using SuiteScript.

Usage Notes

You cannot use SuiteScript to update the Status field on inventory count records. You must set this field by clicking the Start Count button manually on the inventory count record in the UI.

Code Samples

The following samples show how to create an inventory count record with an associated inventory detail record for a lot-numbered or serialized item.

It includes the following:

Step 1: The following sample creates the inventory count record in dynamic mode.

The following sample shows how to create and update inventory cost revaluation records.

          /**

 * @NApiVersion 2.x

 */

define(['N/record'], function(record) {    

    // Script parameters:

    var subsidiaryId = 1;

    var locationId = 6;

    var itemId = 599;

    var binNumber = 501;



    var inventoryCount = record.create({

        type: record.Type.INVENTORY_COUNT,

        isDynamic: true

    });

    inventoryCount.setValue({

        fieldId: 'subsidiary',

        value: subsidiaryId

    });

    inventoryCount.setValue({

        fieldId: 'location',

        value: locationId

    });

    inventoryCount.selectNewLine({

        sublistId: 'item'

    });

    inventoryCount.setCurrentSublistValue({

        sublistId: 'item',

        fieldId: 'item',

        value: itemId

    });

    inventoryCount.setCurrentSublistValue({

        sublistId: 'item',

        fieldId: 'binnumber',

        value: binNumber

    });

    inventoryCount.commitLine({

        sublistId: 'item'

    });

    var recordId = inventoryCount.save();



    log.debug({

        title: 'Inventory Count Created: ' + inventoryCount

    });

}); 

        

Step 2: The following sample adds an Add Inventory Detail button to the record page before it is loaded.

          /**

 * @NApiVersion 2.x

 * @NScriptType UserEventScript

 */

define(['N/ui/serverWidget'], function(serverWidget) {

    function beforeLoad(context) {

        var inventoryCount = context.newRecord;

        var countStatus = inventoryCount.getValue({fieldId: 'status'});

        if (countStatus != 'Started') {

            return;

        }



        var itemLineCount = inventoryCount.getLineCount({sublistId: 'item'});

        var hasInventoryDetail = false;

        for (i = 0; i < itemLineCount; i++) {

            var lineHasInventoryDetail = inventoryCount.hasSublistSubrecord({

                sublistId: 'item',

                fieldId: 'countdetail',

                line: i

            });

            if (lineHasInventoryDetail) {

                hasInventoryDetail = true;

                break;

            }

        }

        if (hasInventoryDetail) {

            return;

        }

        var form = context.form;

        form.addButton({

            id: 'custpage_add_inv_detail',

            label: 'Add Inventory Detail',

            functionName: 'addInventoryDetail()'

        });

    }



    return {

        beforeLoad: beforeLoad

    };

}); 

        

Step 3: The following sample uses an https request to call a Suitelet or RESTlet that will add the inventory detail, and refreshes the page.

          /**

 * @NApiVersion 2.x

 * @NScriptType ClientScript

 *

 * Expects parameters: (configure these in the script deployment)

 *   custscriptrestletscriptid

 *   custscriptrestletscriptdeploymentid

 */

define(['N/runtime', 'N/url', 'N/https', 'N/currentRecord'], function(runtime, url, https, currentRecord) {

    function pageInit(context) {

        // Script parameters

        var script = runtime.getCurrentScript();

        var restletScriptId = script.getParameter({name: 'custscriptrestletscriptid'});

        var restletScriptDeploymentId = script.getParameter({name: 'custscriptrestletscriptdeploymentid'});

        log.debug("Restlet Script ID: " + restletScriptId);

        log.debug("Restlet Deployment ID: " + restletScriptDeploymentId);



        window.addInventoryDetail = function() {

            var inventoryCountId = currentRecord.get().id;

            var requestData = {inventoryCountId: inventoryCountId};

            var headers = {'Content-type': 'application/json'};

            var restletUrl = url.resolveScript({

                scriptId: restletScriptId,

                deploymentId: restletScriptDeploymentId,

                returnExternalId: false

            });

            log.debug('Restlet URL: ' + restletUrl);

            https.put.promise({

                url: restletUrl,

                body: requestData,

                headers: headers

            })

            .then(function(response) {

                var redirectURL = url.resolveRecord({

                    recordType: 'inventorycount',

                    recordId: inventoryCountId,

                    isEditMode: false

                });

                window.open(redirectURL, "_self");

            });

        }

    }



    return {

        pageInit: pageInit

    };

}); 

        

Step 4: The following sample adds the inventory detail subrecord to the inventory count record.

          /**

 * @NApiVersion 2.x

 * @NScriptType Restlet

 *

 * Expects parameters: (configure these in the script deployment)

 *   custscriptcountquantity

 *   custscriptinventorynumber

 */

define(['N/error', 'N/record', 'N/runtime'], function(error, record, runtime) {

    function put(requestBody) {

        // Script parameters

        var script = runtime.getCurrentScript();

        var countQuantity = script.getParameter({

            name: 'custscriptcountquantity'

        });

        var inventoryNumber = script.getParameter({

            name: 'custscriptinventorynumber'

        });

        log.debug({

            details: "Count Quantity: " + countQuantity

        });

        log.debug({

            details: "Inventory Number: " + inventoryNumber

        });



        var inventoryCount = record.load({

            type: 'inventorycount',

            id: requestBody.inventoryCountId

        });



        var itemLineCount = inventoryCount.getLineCount({

            sublistId: 'item'

        });

        for (i = 0; i < itemLineCount; i++) {

            inventoryCount.setSublistValue({

                sublistId: 'item',

                fieldId: 'countquantity',

                line: i,

                value: countQuantity

            });

            inventoryCount.getSublistSubrecord({

                sublistId: 'item',

                fieldId: 'countdetail',

                line: i

            });

            inventoryCount.insertLine({

                sublistId: 'inventorydetail',

                line: 0

            });

            inventoryCount.setSublistValue({

                sublistId: 'inventorydetail',

                fieldId: 'inventorynumber',

                value: inventoryNumber,

                line: 0

            });

            inventoryCount.setSublistValue({

                sublistId: 'inventorydetail',

                fieldId: 'quantity', value:

                countQuantity,

                line: 0

            });

            var recordId = inventoryCount.save();

        }

    }



    return {

        put: put

    };

}); 

        

Related Topics

Inventory Count
Working with the SuiteScript Records Browser
SuiteCloud Supported Records
Transactions
Setting Up Inventory Count
Creating Manual Inventory Counts
Working with an Inventory Count
Basic Inventory Management

General Notices