ericzombie
ericzombie

Reputation: 41

How go I get csv data into netsuite?

I've got an update to my question. What I really wanted to know was this: How do I get csv data into netsuite? Well, it seems I use the csv import tool to create a mapping and use this call to import the csv nlapiSubmitCSVImport(nlobjCSVImport).

Now my question is: How do I iterate through the object?! That gets me half way - I get the csv data but I can't seem to find out how I iterate through it in order to manipulate the date. This is, of course, the whole point of a scheduled script.

This is really driving me mad.

@Robert H I can think of a million reasons why you'd want to import data from a CSV. Billing, for instance. Various reports on data any company keeps and I wouldn't want to keep this in the file cabinet nor would I really want to keep the file at all. I just want the data. I want to manipulate it and I want to enter it.

Upvotes: 4

Views: 6664

Answers (3)

Amit
Amit

Reputation: 2565

Solution Steps:

  1. To upload a CSV file we have to use a Suitelet script.

    (Note: file - This field type is available only for Suitelets and will appear on the main tab of the Suitelet page. Setting the field type to file adds a file upload widget to the page.)

    var fileField = form.addField('custpage_file', 'file', 'Select CSV File');
    var id = nlapiSubmitFile(file);
    
  2. Let's prepare to call a Restlet script and pass the file id to it.

    var recordObj = new Object();
    recordObj.fileId = fileId;
    
    // Format input for Restlets for the JSON content type
    var recordText = JSON.stringify(recordObj);//stringifying JSON
    
    // Setting up the URL of the Restlet
    var url = 'https://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=108&deploy=1';
    
    // Setting up the headers for passing the credentials
    var headers = new Array();
    
    headers['Content-Type'] = 'application/json';      
    headers['Authorization'] = 'NLAuth [email protected], nlauth_signature=*password*, nlauth_account=TSTDRV****, nlauth_role=3';
    

    (Note: nlapiCreateCSVImport: This API is only supported for bundle installation scripts, scheduled scripts, and RESTlets)

  3. Let's call the Restlet using nlapiRequestURL:

    // Calling Restlet
    var output = nlapiRequestURL(url, recordText, headers, null, "POST");
    
  4. Create a mapping using Import CSV records available at Setup > Import/Export > Import CSV records.

  5. Inside the Restlet script Fetch the file id from the Restlet parameter. Use nlapiCreateCSVImport() API and set its mapping with mapping id created in step 3. Set the CSV file using the setPrimaryFile() function.

    var primaryFile = nlapiLoadFile(datain.fileId);
    var job = nlapiCreateCSVImport();
    job.setMapping(mappingFileId); // Set the mapping
    
    // Set File
    job.setPrimaryFile(primaryFile.getValue()); // Fetches the content of the file and sets it.
    
  6. Submit using nlapiSubmitCSVImport().

    nlapiSubmitCSVImport(job); // We are done
    

There is another way we can get around this although neither preferable nor would I suggest. (As it consumes a lot of API's if you have a large number of records in your CSV file.)

Let's say that we don't want to use the nlapiCreateCSVImport API, so let's continue from the step 4.

  1. Just fetch the file Id as we did earlier, load the file, and get its contents.

    var fileContent = primaryFile.getValue();
    
  2. Split the lines of the file, then subsequently split the words and store the values into separate arrays.

    var splitLine = fileContent.split("\n"); // Splitting the file on the basis of lines.
    
    for (var lines = 1,count=0; lines < splitLine.length; lines++)
    {
        var words = (splitLine[lines]).split(","); // words stores all the words on a line
    
        for (var word = 0; word < words.length; word++)
        {
            nlapiLogExecution("DEBUG", "Words:",words[word]);
        }
    }
    

    Note: Make sure you don't have an additional blank line in your CSV file.

  3. Finally create the record and set field values from the array that we created above.

    var myRec = nlapiCreateRecord('cashsale'); // Here you create the record of your choice
    
    myRec.setFieldValue('entity', arrCustomerId[i]); // For example, arrCustomerId is an array of customer ID.
    
    var submitRec = nlapiSubmitRecord(myRec); // and we are done
    

Upvotes: 3

felipechang
felipechang

Reputation: 924

CSV to JSON:

If you know the structure of the CSV file, just do a for loop and map the fields to the corresponding nlapiSetValue.

Should be pretty straightforward.

Upvotes: 0

Yannick
Yannick

Reputation: 194

fellow NetSuite user here, I've been using SuiteScripts for a while now but never saw nlobjCSVImport object nor nlapiSubmitCSVImport .. I looked in the documentation, it shows, but there is no page describing the details, care to share where you got the doc from?

With the doc for the CSVImport object I might be able to provide some more help.

P.S. I tried posting this message as a comment but the "Add comment" link didn't show up for some reason. Still new to SOF

Upvotes: 0

Related Questions