Read and Extract Text from Documents with NetSuite’s N/documentCapture Module

What is the N/documentCapture Module?

The N/documentCapture module in NetSuite helps you automatically extract data from documents like PDFs, invoices, and images. This module can pull out key information like invoice numbers, dates, amounts, and more — and put it directly into your NetSuite system.

This means less manual data entry and fewer chances for mistakes, saving you time and improving accuracy.

How Does It Work?

The N/documentCapture module helps automate the process of pulling text from a document and transferring that data into the relevant fields in NetSuite.

Here’s a simple breakdown of how it works:

  1. Load the Document: First, you load the document (e.g., PDF or image) into NetSuite’s File Cabinet.
  2. Extract the Data: Using the documentToText() or documentToStructure() functions, the module extracts relevant text and key-value pairs from the document.
  3. Store the Data: The extracted data is then used to update your NetSuite records (e.g., filling in invoice numbers, amounts, and other details).

Example Script: Using N/documentCapture to Extract Invoice Data

Here’s a simple SuiteScript example that shows how you can use the N/documentCapture module to extract data from a PDF and use it to update a Vendor Bill in NetSuite:

/**
 * @NApiVersion 2.1
 * @NScriptType ScheduledScript
 */
define(['N/documentCapture', 'N/file', 'N/log'],
    /**
   * @param{record} record
   * @param{recordContext} recordContext
   */
    (documentCapture, file, log) => {
        "use strict";
        /**
         * Defines the Scheduled script trigger point.
         * @param {Object} scriptContext
         * @param {string} scriptContext.type - Script execution context. Use values from the scriptContext.InvocationType enum.
         * @since 2015.2
         */
        function execute(context) {
            try {
                var fileId = 1410583; // The ID of the file you saved in the File Cabinet


                // Load the PDF from the File Cabinet
                var pdfFile = file.load({
                    id: fileId
                });


                // Extract text from the PDF file
                var extractedText = documentCapture.documentToText({
                    file: pdfFile
                });
                // Log the extracted text
                log.debug('Extracted PDF Text', extractedText);


                // Example: Extract specific values from the text (e.g., BOL No, invoice number)
                var BOLNumber = extractBolNo(extractedText);
                log.debug('BOL Number Extracted', BOLNumber);
            } catch (e) {
                log.error('Error processing PDF', e.message);
            }
        }


        // Custom function to extract BOL Number from the extracted text
        function extractBolNo(text) {
            // Regex to capture BOL number
            var regex = /BOLs+NO:s*([A-Za-z0-9]+d+)/i;
            var match = text.match(regex);


            if (match && match[1]) {
                return match[1]; // Return the extracted BOL number (e.g., xyz2445362)
            }


            return null; // Return null if no match is found


        }
        return { execute }
    });

Leave a comment

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