Bud
Bud

Reputation: 781

SuiteScript - Open, Read File

This is going to be a really stupid question - how do I open a file in the Filing Cabinet and read it in, line by line, using SuiteScript? Every example I can find online seems to start in the middle, taking for granted knowledge that I don't possess.

Is there a simple example somewhere online I've not found? All I need is for it to:

Upvotes: 0

Views: 4186

Answers (3)

Aamer
Aamer

Reputation: 622

Let's keep it simple:

For SuiteScript 1.0:

var arrLines = nlapiLoadFile({fileinternalid}).getValue().split(/\n|\n\r/);

For SuiteScipt 2.0:

var arrLines = file.load({
    id: {fileinternalid}
});
arrLines.getValue().split(/\n|\n\r/);
arrLines.description = 'My CSV File'
var fileId = arrLines.save();
...
// Add additional code

Upvotes: 0

Martha
Martha

Reputation: 764

In Suitescript 2.0 use the N/file module. The module can only be used in server side (not client) scripts. Reference Suite Answer Id: 43524 for N/file module and Suite Answer Id: 43520 for Script Types.

require(['N/file', 'N/record'], function(file, record) {
    //use file name and folder and 'N/search' module to find the file id if necessary

    // load file
    var myFile = file.load({
        id: '__' //enter the file internal id, absolute or relative file path
    })
   //get the # of lines
   var arrLines = myFile.getContents().split(/\n|\n\r/);
    // loop through each line, skipping the header
    for (var i = 1; i < arrLines.length - 1; i++) {
        //split the 1 line of data into an array.  If the file was a csv file, each array position holds a value from the columns of the 1 line of data
        var content = arrLines[i].split(',');

        // get values from the columns of a CSV file 
        var column1 = content[0]; //first column
        var column2 = content[1]; //second column
        //can use the column data above to i.e. create new record and set default value, update existing records, write the data elsewhere


        //to check each line for a given value
        arrLines[i].includes('keyword');  //returns true or false
    }

});

Upvotes: 2

Brian Duffy
Brian Duffy

Reputation: 162

You can get the suitescript api documentation here

https://docs.oracle.com/cloud/latest/netsuitecs_gs/docs.htm

look at the file module

Upvotes: 0

Related Questions