Learn how to submit a CSV Import using SuiteScript

Introduction

This article will describe how to use SuiteScript to create a task to import a CSV file.

I wrote an article a few weeks back about how to upload and download files using the sFTP module:

A common use case is to download a file using sFTP and then import it by creating a CSV Import task.  Let’s dive in.

Step 1: Create the CSV Import Job

You will first need to create a CSV Import job which you are going to use to import the records.  Go to Setup->Import/Export->Import CSV Records and create a CSV Import.  I created a simple sales order CSV import.  This article will not go into detail about how to do this, review help if you need to.

Go to Setup->Import/Export->Saved CSV Imports and note the mapping ID of the import you created.

 

 

Step 2: Add and Deploy Script

 

Here is the scheduled script which searches a directory for files and then creates a CSV Import task for each of them to import the records.  You will need to modify the filter to use the internal ID of the folder you are using to store the CSV files.  You will also need to change the mapping ID to the one you created.

 

/**
 *@NApiVersion 2.1
 *@NScriptType ScheduledScript
 */

define([ 'N/search', 'N/file', 'N/task'],
 (search, file, task) => {

	execute = (context) => {
        log.debug('==START==','==START==');
        try {
            const allFiles = getFiles();
            for(let fileId of allFiles) {
                // create CSV import task
                const scriptTask = task.create({taskType: task.TaskType.CSV_IMPORT});
                // change this to your mapping ID
                scriptTask.mappingId = 116;
                const csvFile = file.load(fileId);
                scriptTask.importFile = csvFile;
                // submit csv import job
                const csvImportTaskId = scriptTask.submit();
                log.debug('CSV task submitted ID', csvImportTaskId);
            }
        }
        catch(e) {
            log.error('Error', JSON.stringify(e));
        }
    }

    getFiles = () => {
        const folderSearchObj = search.create({
            type: "folder",
            filters: [
                // change this ID to the folder ID where the CSV files are
                ["internalidnumber","equalto", 655]
            ],
            columns: [
                search.createColumn({
                    name: "internalid",
                    join: "file",
                    label: "Internal ID"
                })
            ]
        });
        let allFiles = [];
        folderSearchObj.run().each(function (result) {
            let internalid = result.getValue({
                name: "internalid",
                join: "file",
                label: "Internal ID"
            });
            allFiles.push(internalid);
            return true;
        });
        return allFiles;
    }

    return {execute};

});

Add and deploy the script.  Reference my article “Quick Guide to Adding and Deploying a Script in NetSuite” if needed.

Create a CSV file for your import and drop it into the the folder you created. Run the script.  Now go to the CSV Job Status page by going to Setup->Import/Export->View CSV Import Status.  You will see the status of your import.

 

 

Homework

You could improve this script by using script parameters for the CSV folder ID and the mapping ID. You can also create a multi file CSV Import and then use scriptTask.linkedFiles to import them. Review the NetSuite help to get more information about using scriptTask.linkedFiles.

Conclusion

This article demonstrates one specific task which is part of the N/task module.  I encourage you to review the other tasks that are available. It’s also worth noting that the CSV Import task can only be submitted from bundle installation scripts, scheduled scripts, and RESTlets.

If you need help with customizing NetSuite, please contact Suite Tooth Consulting here to set up a free consultation.

If you liked this article, please sign up for my newsletter to get these delivered to your inbox here.

Follow on
Jaime Requena Chief Executive Officer

Jaime Requena is a seasoned NetSuite Consultant and Solutions Architect, known for delivering WHITE GLOVE service to businesses. With 15+ years of experience and 3x certifications in ERP, Developer, and Admin, Jaime specializes in highly customized NetSuite accounts, transforming operations for 200+ satisfied customers all across the globe.

Get Connected

How can we help?


    Stay in the loop with Suite Tooth Consulting!

    We aim to bring unmatched expertise and professionalism to your NetSuite initiatives. Let’s talk about how our NetSuite consultancy can make a difference!

    Global Client Satisfaction

    Client Testimonials

    It’s Been 4+ Years Now And We Have Worked With Hundreds Of Clients, Building Our Way To The Top, One Happy Client After Another! Their Voices Of Satisfaction Serve As A Testament To Our Success –