'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:

  • Open file (giving file name and folder)
  • Read the first line
  • Read the second line
  • ....
  • Close the file.


Solution 1:[1]

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
    }

});

Solution 2:[2]

You can get the suitescript api documentation here

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

look at the file module

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martha
Solution 2 Brian Duffy