Inventory Count

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.1
 * @NScriptType ClientScript
 */
  
define(['N/record', 'N/log'], (record, log) => {
  /**
   * @param {Object} context Defines the function that is executed
   * when the page completes loading or when the form is reset.
   */
  function pageInit (context) {
    // Script parameters:
    const subsidiaryId = 1  
    const locationId = 6   
    const itemId = 599   
    const binNumber = 501
    const 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'  
    })   
    inventoryCount.save() 
    log.debug({
      title: 'Inventory Count Created: ' + inventoryCount
    })
  } return {
    pageInit: pageInit
  }
}) 

        

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

          /**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
 
define(['N/ui/serverWidget'], (serverWidget) => {
  /**
   * @param {Object} context Defines the function that is executed before a
   * record is loaded; that is, whenever a read operation occurs on a record,
   * and prior to returning the record or page.
   */
  function beforeLoad (context) {
    const inventoryCount = context.newRecord    
    const countStatus = inventoryCount.getValue({ fieldId: 'status' })     
    if (countStatus !== 'Started') {
      return
    }   
    const itemLineCount = inventoryCount.getLineCount({ sublistId: 'item' })
    let hasInventoryDetail = false
    for (let i = 0; i < itemLineCount; i++) {
      const lineHasInventoryDetail = inventoryCount.hasSublistSubrecord({  
        sublistId: 'item',
        fieldId: 'countdetail',
        line: i
      })
      if (lineHasInventoryDetail) {
        hasInventoryDetail = true
        break
      }
    }
    if (hasInventoryDetail) {
      return
    }
    const 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.1
 * @NScriptType ClientScript
 */
  
define(['N/runtime', 'N/url', 'N/https', 'N/currentRecord', 'N/log'], (runtime, url, https, currentRecord, log) => {
  /**
   * @param {Object} context Defines the function that is executed
   * when the page completes loading or when the form is reset.
   *
   * @param {String} custscriptrestletscriptid Custom script parameter
   * configured in script deployment.
   *
   * @param {String} custscriptrestletscriptdeploymentid Custom script
   * parameter configured in script deployment.
   */
  function pageInit (context) {
    const script = runtime.getCurrentScript()
    const restletScriptId = script.getParameter({
      name: 'custscriptrestletscriptid'
    })
    const restletScriptDeploymentId = script.getParameter({
      name: 'custscriptrestletscriptdeploymentid'
    })
    log.debug('Restlet Script ID: ' + restletScriptId)     
    log.debug('Restlet Deployment ID: ' + restletScriptDeploymentId)
       
    window.addInventoryDetail = function () {
      const inventoryCountId = currentRecord.get().id
      const requestData = { inventoryCountId: inventoryCountId }
      const headers = { 'Content-type': 'application/json' }
      const restletUrl = url.resolveScript({
        scriptId: restletScriptId,
        deploymentId: restletScriptDeploymentId,
        returnExternalId: false 
      })
      log.debug('Restlet URL: ' + restletUrl)
      https.put.promise({
        url: restletUrl,
        body: requestData,
        headers: headers
      }).then((response) => {
        const 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.1
 * @NScriptType Restlet
 */
define(['N/record', 'N/runtime', 'N/log'], (record, runtime, log) => {
  /**
    * @param {Object} requestBody Pass the request body as a JavaScript Object
    * when the request Content-Type is 'application/json' or 'application/xml'.
    *
    * @param {number} custscriptcountquantity Custom script parameter configured
    * in script deployment.
    *
    * @param {number} custscriptinventorynumber Custom script parameter
    * configured in script deployment.
    */
  function put (requestBody) {
    const script = runtime.getCurrentScript()
    const countQuantity = script.getParameter({
      name: 'custscriptcountquantity'
    })
    const inventoryNumber = script.getParameter({ 
      name: 'custscriptinventorynumber'
    })
    log.debug({ 
      details: 'Count Quantity: ' + countQuantity
    })
    log.debug({ 
      details: 'Inventory Number: ' + inventoryNumber
    })
    const inventoryCount = record.load({
      type: 'inventorycount',
      id: requestBody.inventoryCountId
    })
    const itemLineCount = inventoryCount.getLineCount({ 
      sublistId: 'item'
    })
    for (let i = 0; i < itemLineCount; i++) {
      inventoryCount.setSublistValue({
        sublistId: 'item',
        fieldId: 'countquantity',
        line: i,
        value: countQuantity
      })
      const countDetail = inventoryCount.getSublistSubrecord({       
        sublistId: 'item',
        fieldId: 'countdetail',
        line: i
      })
      countDetail.insertLine({
        sublistId: 'inventorydetail',
        line: 0
      })
      countDetail.setSublistValue({
        sublistId: 'inventorydetail',
        fieldId: 'inventorynumber',
        value: inventoryNumber,
        line: 0
      })
      countDetail.setSublistValue({
        sublistId: 'inventorydetail',
        fieldId: 'quantity',
        value: countQuantity,
        line: 0
      })
      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