Creating the CSV file contains the current month created customer details and sending the CSV File to the NetSuite Admin using script

Using the Scheduled script, because I need to send a csv file report every month to the NetSuite admin,

/**

 * @NApiVersion 2.1

 * @NScriptType ScheduledScript

 */

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

  /**

 * @param{email} email

 * @param{file} file

 * @param{record} record

 * @param{search} search

 * @param{runtime} runtime

 */

  (email, file, record, search, runtime) => {

    /**

     * Defines the Scheduled script trigger point.

     * @param {Object} scriptContext

     * @param {string} scriptContext.type – Script execution context. Use values from the scriptContext.InvocationType enum.

     * @since 2015.2

     */

    const execute = (scriptContext) => {

      try{

        //search create

        let customerSearch = search.create({

          type : search.Type.CUSTOMER,

          filters : [‘datecreated’,‘within’,‘thismonthtodate’],

          columns : [‘companyname’,‘datecreated’,‘salesrep’,‘terms’]

        });

        //Innitialize csv content

        let csvContent = ‘Name,Date Created,Sales Rep,Termsn‘

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

          let customerName = result.getValue(‘companyname’);

          let date = result.getValue(‘datecreated’);

          let salesRep = result.getValue(‘salesrep’);

          let terms = result.getValue(‘terms’);

          csvContent += customerName +‘,’+date +‘,’+salesRep +‘,’+terms +‘n‘

          return true;

        });

        //Craete CSV File

        let csvFile = file.create({

          name : ‘customers_name.csv’,

          contents : csvContent,

          folder : 613,

          fileType : ‘CSV’

        });

        let csvSave = csvFile.save();

        log.debug(“CSV created successfully”,csvSave);

        //Get admin

        let admin = runtime.getCurrentUser();

        let adminId = admin.id;

        log.debug(“Admin ID”,adminId);

        //Email send

        email.send({

          author : 2026, //Any sales rep internal ID

          recipients : adminId,

          subject : “This month created customer”,

          body : “This attached csv contains this month created customers with details”,

          attachments : [csvFile]

        });

        log.debug(“Email send successfully”);

      }catch(e){

        log.error(“CSV not created”,e.message);

      }

    }

    return {execute}

  });

Leave a comment

Your email address will not be published. Required fields are marked *