Export saved search result into csv and save in file cabinet

Export saved search result into csv and save in file cabinet

Category : Uncategorized

In this blog of smiling coders we will be talking about saved search,file object and its automation .  I Suppose you have a client asking for Exporting a saved search result and save it as CSV file to the File Cabinet that task we can automate via script.

 To get the value of each columns, resultObject.getValue(name: ‘value’) can be used where the internal id of the field is required. In this article, the internal ids will not be use.

Approach

To get the value of the columns without using the internal id of the field, resultObject.getValue(mySearch.columns[0]) can be used instead.

Consider the following details:

Saved Search ID: 123 //any integer

Saved Search Columns:

 • Internal ID

 • Item Name

 • Formula Column

Folder ID: 123 //folder internal id

With these details, see the sample Scheduled Script below.

/** 

* @NApiVersion 2.x 

* @NScriptType ScheduledScript 

* @NModuleScope SameAccount 

*/

define([‘N/search’,’N/file’], function(search,file) {

function execute(scriptContext) {

              //Load saved search

              var mySearch = search.load({

              id: ‘123’

              })

              //Run saved search

              var SCSearchResultSet = mySearch.run();

              log.debug(‘Results…’,SCSearchResultSet)

              //Headers of CSV File separated by commas and ends with a new line (\r\n)

              var csvFile = ‘Internal ID,Item Name,Average Cost\r\n’;

              //Iterate through each result

              SCSearchResultSet.each(iterator);

              function iterator(resultObject){

              //Get value returned for Internal ID

              var internalid = resultObject.getValue(mySearch.columns[0])

              //Get value returned for Item ID

              var itemid = resultObject.getValue(mySearch.columns[1])

              //Get value returned for Formula Column

              var formulacolumn = resultObject.getValue(mySearch.columns[2])

              //Add each result as a new line on CSV

              csvFile += internalid+’,’+itemid+’,’+formulacolumn+’\r\n’

              return true;

              }

              //Variable for datetime

              var date = new Date();

              //Creation of file

              var fileObj = file.create({

              //To make each file unique and avoid overwriting, append date on the title

              name: ‘Saved Search Result – ‘ + date.toLocaleDateString() +’.csv’,

              fileType: file.Type.CSV,

              contents: csvFile,

              description: ‘This is a CSV file.’,

              encoding: file.Encoding.UTF8,

              folder: 123

              });

              //Save the CSV file

              var fileId = fileObj.save()

              log.debug(‘File ID…’,fileId)

    }

    return {

        execute: execute

    };

});

Reference : netsuite.com, netsuite help  and netsuite guide.

Feel free to reach out Netsuite Solution provider  TheSmilingCoders for proper netsuite implementationnetsuite consultation , netsuite customizationnetsuite Supportnetsuite Training NetSuite Celigo Integration , Celigo ConsultantCeligo Integration Consultant.

contact us : [email protected]

Facebook : https://www.facebook.com/Smiling-Coders-Netsuite-Training-Implementation-and-Support-1591053827660082/

Linkedin : https://www.linkedin.com/company/netsuite-academy/