Tip – 45: Pros & Cons of CSV Import through SuiteScript different versions in NetSuite

In my last two blogs(Tip 43 & Tip 44), we saw, how to write scripts to do CSV Import job using SuiteScript in different versions.

In this blog, we will be comparing both Suitescript versions with the same task and when to use which version.

SuiteScript 1.0SuiteScript 2.0
You will get Task ID in Numeric format in response code.You will get task ID as a combination of some random characters on submitting the task.
Task ID can be used to get CSV Response File link.This task ID can’t be used get CSV Response File link.
You can’t check status of the job in this version.Provides API to check status of the task. But here is one problem, the Status says “COMPLETED”, even if the CSV Import is not successful. We will discuss about it more below.

So, now we saw different features provided by NetSuite in different versions.

Now let’s think about how to decide which version of code we should go with.

My answer to this question is analyze the requirement first.

  1. If requirement is just to execute CSV Import through script, and client is not worried about responses/status, then you can go with either version.
  2. If requirement is like user wants to know about the Response details like, whether all records imported correctly or not, and they want check that by downloading the response file, you should go ahead with SuiteScript 1.0. You can check my blog(https://ashabarijena.wordpress.com/2023/07/10/tip-44-fetch-csv-import-response-file-link-after-csv-import-using-suitescript/) about how to achieve that. Also, note that SuiteScript 1.0 doesn’t give you task status details. While sending Response file link, let the user know, she/he may need to wait for sometime to download the response file, in case she/he gets any error.
  3. If the requirement is like, send an update email or do something else, once the CSV Import job is finished, then go ahead with SuiteScript 2.0.

Suggestion to NetSuite: Here I feel little weird regarding the Job status text that we get. Irrespective of the CSV Import creates/updates all records successfully or not, we get status code as “COMPLETED”.

It would have been great, if for unsuccessful job completion, we could get a different status name like “UNSUCCESSFUL” or something like that. At least looking into that status, user will get to know, for this job, not all records have not been through. So let me go ahead and check the response file and figure out the issue. As current feature only provides “COMPLETED”, user has to go to Job status page every time to see which one is successful and which one is not.

Let me know, what do you think.

NOTE: There is an enhancement ticket() raised for adding feature to SuiteScript 2.0 to provide APIs for downloading CSVresponse file. Please vote for that, so that we can have the feature earlier.

Enhancement details: Enhancement 366617: SuiteScript: Import/Export > View CSV Import Status > Ability to get the CSV Response files using script and store them in file cabinet

Hope this blog will help you while deciding which version of code, I should be using.

Happy Coding 🙂

Thank You

Asha

Tip: 44 – Fetch CSV Import Response file link after CSV Import using SuiteScript

In my recent project, I got a requirement to send CSV Response file to some users through email once Schedule CSV Import is done using SuiteScript.

Existing NetSuite Limitation: As you have noticed, when user does CSV Import manually, once the CSV Import is finished, an email is sent to that user with summary of records processed. This is done by NetSuite by default. But when CSV Import is done using Suitescript, no email is received. As the script is executed as Administator and NetSuite server takes it as executed by system. So no email is sent.

The 2nd problem is when we go to Job status by navigating, Setup > Impost/Export > View CSV Import Status , we can see a column about CSV Response. On click of the link on any line, a response file is getting downloaded. But there is no way we can download that file using SuiteScript, because the file in not available in File Cabinet.

Now, as the client wants to see the response file after every CSV Import through SuiteScript, we thought of providing at least a link in the email to make it downloadable.

But then comes our next challenge. In SuiteScript 2.0, we can’t provide any dynamic link as the task id that we get, isn’t same as we get the URL of the link of the file.

The File link usually looks like: “https://<NETSUITE_ACCOUNT_ID&gt;.app.netsuite.com/app/setup/upload/csv/uploadlogcsv.nl?wqid=<TASK_ID>”

So, if we get the task ID, we can have the link of that file.

But, as the task ID we get from SUiteScript 2.0 is not same as task ID that we need for file link, we can’t use SuiteScript 2.0 to get Response file link.

SuiteScript 1.0 allows that. So, here is the below sample code to achieve this.

function RecordUpdateByCSVImport(type) {

var CSV_RESPONSE_URL = "https://<NS_ACCOUNT_ID>.app.netsuite.com/app/setup/upload/csv/uploadlogcsv.nl?wqid=";

//Load File 
var fileObj = nlapiLoadFile(fileId);
var fileName = fileObj.name;

//Create CSV Import Task
var job = nlapiCreateCSVImport();
job.setMapping(soImportId);
job.setPrimaryFile(fileObj);
job.setOption("jobName", "CSV_Import_" + fileName);

var responseCode = nlapiSubmitCSVImport(job);
nlapiLogExecution('DEBUG', 'TASK ID', responseCode);

var csvResponseURL = CSV_RESPONSE_URL + "" + responseCode;
nlapiLogExecution('DEBUG', 'csvResponseURL', csvResponseURL);                       

var body = "<p>CSV Import is just executed. Hele is the link to CSV Response File <a>" + CSV_RESPONSE_URL "</a> Login to NetSuite and click the link to download the file for analysis.</p>";
nlapiLogExecution('DEBUG', 'Job ID or error:\n', responseCode);
nlapiSendEmail(-5, 'test@gmail.com', 'CSV job details', body);

}

We can save the link in a custom filed in, if you have a requirement of it.

NOTE: Please note, using this link, we can’t download the file through script. Its not allowed in NetSuite. We need to click the link to make it download.

If you have similar requirement, I am sure this blog will help you.

Happy Coding 🙂

Thank you

Asha

Tips – 43: CSV Import using SuiteScript in NetSuite

Hello Friends,

While working on a recent project, I have been through many challenges while working on CSV Import using Suitescript. I will be posting different blogs for different implementation and challenges and will have one summary blog for overall issues that we found recently with NetSuite limitations.

In this blog, I will start with simple process of how to do CSV Import using SuiteScript.

I will provide sample codes in both SuiteScript 1.0 & SuiteScript 2.0 & SuiteScript 2.1, so that you can use as per your need. I usually get responses from you, what will be the code in other versions on blogs that I post with scripts with one versions only.

Steps to achieve CSV Import using SuiteScript, assuming you know, how to create a saved CSV import and you have basic knowledge on SuiteScript 1.0 & SuiteScript 2.0.

Step 1: Create a Saved CSV Import in Netsuite.

For this step make sure you have already a CSV file, which will be used to map fields between CSV file and NetSuite record. I will have another blog just to show how to create a saved CSV Import.

Once you create the Saved CSV Import record, get the Saved CSV Import record ID (let’s say “custimport_test_csvimport“).

NOTE: I always recommend users to give proper name to internal ids of any record/fields/saved search you are creating. As once you move your code to some other account, ids are going to be changed, and you need to change those in your scripts as well.

Step 2: Move File to File Cabinet:

Upload the CSV File(which you are going to import) in File Cabinet and get the File ID(let’s say “12345”)

Step 3: Create SuiteScript file (Suitescript 1.0)

Depending upon when and how do you want the CSV Import will be done, create type of script as per your need.

I am creating a Scheduled script here with the scenario that, my client wants to execute CSV Import to be done automatically everyday once.

Now I will show both in SuiteScript versions on how to do it.

SuiteScript 1.0:

Script Name: SS_CSVimport_V1.js

function RecordUpdateByCSVImport(){

               //Load File
               var fileObj = nlapiLoadFile("12345");
               var fileName = fileObj.name;
               nlapiLogExecution('DEBUG', 'fileName', fileName);
                //Create CSV Import Task
                var job = nlapiCreateCSVImport();
                job.setMapping("custimport_test_csvimport");
                job.setPrimaryFile(fileObj);
                job.setOption("jobName", "CSV_Import_"+fileName);

                var  responseCode = nlapiSubmitCSVImport(job);
                nlapiLogExecution('DEBUG', 'TASK ID', responseCode);

}

Step 4: Create Schedule Script in version 1.0

Go to Customization > Scripting > New > Upload script file > set Script Record Name, Function Name, script ID > Deploy Script. Now you are ready to test.

Make Sure you deployment status should be either Testing/Not Scheduled for testing on demand.

Now we will repeat Step 3 by creating SuiteScript in version 2.0.

Step 3: Script Script File: SS_Update_Record_By_CSV_Import.js

/**

@NApiVersion 2.0

@NScriptType ScheduledScript
*/
define(“N/file”, “N/record”, “N/task”, function (file, record, task){

function RecordUpdateByCSVImport(scriptContext){

// load the file var fileObj = file.load("12345");

var fileName = fileObj.name;

log.debug({ title: "fileName:", details: fileName });

var today = new Date();

//Create t ask for savedCSVimport for loaded file Object

var importTask = task.create({

taskType: task.TaskType.CSV_IMPORT,

mappingId: custimport_test_csvimport,

importFile: fileObj,

name: fileName + "_" + today

});

//Submit the task

var csvImportTaskId = importTask.submit();

log.debug({ title: "csvImportTaskId:", details: csvImportTaskId });

//Get task status

var csvTaskStatus = task.checkStatus({ taskId: csvImportTaskId });

log.debug({ title: "csvTaskStatus:", details: csvTaskStatus });

if (csvTaskStatus.status == task.TaskStatus.COMPLETED)

log.debug("Import Task completed for fileId = " + fileId, "TASK finished");}

return {

execute: RecordUpdateByCSVImport

}

});

Step 4: Create Scheduled Script Record using new Suitescript file and execute on demand to test, if its working on not.

I am also providing code using Suitescript 2.1, in case anyone is writing code in SuiteScript 2.1.

Here is the code:

/**

  • @NApiVersion 2.1
  • @NScriptType ScheduledScript
    */

let N_FILE = {};
let N_RECORD = {};
let N_SEARCH = {};
let N_TASK = {};

let DEFINE_ARRAY = [];
DEFINE_ARRAY.push(“N/file”);
DEFINE_ARRAY.push(“N/record”);
DEFINE_ARRAY.push(“N/search”);
DEFINE_ARRAY.push(“N/task”);
DEFINE_ARRAY.push(“N/runtime”);
define(DEFINE_ARRAY,
(file, record, search, task, runtime) => {
N_FILE = file;
N_RECORD = record;
N_SEARCH = search;
N_TASK = task;

    const execute = (scriptContext) => {

                    // load the file
                    let fileObj = N_FILE.load("12345");
                    let fileName = fileObj.name;

                    var today = new Date();
                    //Create task for savedCSVimport for loaded file Object
                    let importTask = N_TASK.create({
                        taskType: N_TASK.TaskType.CSV_IMPORT,
                        mappingId: custimport_test_csvimport,
                        importFile: fileObj,
                        name: fileName+ "_CSVIMport_" + today
                    });

                    //Submit the task
                    let csvImportTaskId = importTask.submit();
                    log.debug({ title: "csvImportTaskId:", details: csvImportTaskId });

                    //Get task status
                    let csvTaskStatus = N_TASK.checkStatus({
                        taskId: csvImportTaskId
                    });
                    log.debug({ title: "csvTaskStatus:", details: csvTaskStatus });

                    if (csvTaskStatus.status == N_TASK.TaskStatus.COMPLETED) 

                        log.debug("Import Task completed for fileId = " + fileId, "Task Completed");

    }

    return { execute }
});

I will have more follow up blogs related to CSV Import.

Hope this blog will help you.

Happy Coding 🙂

Thank You

Asha