Email Search Results in CSV File using SuiteScript

Category : Uncategorized

You can export the result of a saved search to a CSV file in the User Interface by clicking on the Export (CSV) link on the list of Saved Searches, however, this requires User interaction requiring the User to log in and initiate this process.Furthermore, Scheduled Scripts provide more frequency options than the options available when sending the search results in the User Interface.

Using a Scheduled Script, Suitescript 2.0 with the sample code below, a Customer search is created and its result are stored in a CSV file and sent as an attachment in an email to a specified recipient.

/**

 *@NApiVersion 2.0

 *@NScriptType ScheduledScript

 */

define([‘N/file’, ‘N/search’,’N/record’,’N/email’],

function(file,search,record,email) {

function sendEmail() {

              //creating search

              var myCustomerSearch = search.create({

                             type: search.Type.CUSTOMER,

                             columns: [{

                                           name: ‘internalid’

                             }, {

                                           name: ’email’

                             }, {

                                           name: ‘phone’

                             }],

                             filters: [{

                                           name: ‘lastname’,

                                           operator: ‘startswith’,

                                           values: [‘s’]

                             }]

              });

              var myContent = ”;

              //looping through search result

              if(myCustomerSearch){

                              myCustomerSearch.run().each(function(result) {

              var internalID = result.getValue({

                             name: ‘internalid’

              });

              var email = result.getValue({

                             name: ’email’

              });

              var phone = result.getValue({

                            name: ‘phone’

              });

              myContent += internalID + ‘,’ + email + ‘,’ + phone + ‘\n’;

           return true;

         });

      //creating CSV file

              var fileObj = file.create({

                                           name: ‘test.csv’,

                                           fileType: file.Type.CSV,

                                           contents: myContent

                             });

     //sending email

              email.send({

                             author: <author>,

                             recipients: <recipient>,

                             subject: ‘Test Sample Email Module’,

                             body: <email body>,

                             attachments: [fileObj],

                             relatedRecords: {

                             entityId: <entityid>

                             }

                             });

              }

}

return{

              execute: sendEmail

};

});

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 : info@smilingcoders.com

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

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