Sample map-reduce script to list all custom records with file attachments in NS account

The requirement was to find all the custom record types in the NetSuite accounts that have file attachments. The custom record type names were taken from the record types page. Here is a basic map-reduce script for listing all the custom records with file attachment count. I have also added code to save the result as a CSV file to overcome the script execution log limit in the UI.

/**

 * @NApiVersion 2.1

 * @NScriptType MapReduceScript

**/

/*************************************************************************************************************************

 * * * PRQT-2326 - NetSuite-Google Drive Integration

 * **********************************************************************************************************************

 * * *

 * Author: Jobin & Jismi IT Services LLP

 *

 * Date Created : 20-03-2024

 * Instance | ViewPoint Sandbox

 * Created By: JJ0178 , Jobin & Jismi IT Services LLP

 * Details: 

 * REVISION HISTORY

 *  

 */

define(['N/search', 'N/file'], (search, file) => {

  "use strict"

  const FOLDER_ID = '####'; // Folder Id to save the result CSV file

  function getInputData() {

    try {

      var customRecordTypes = [

			'customrecord_record1',

			'customrecord_record2'

           ];

      return customRecordTypes;

    } catch (error) {

      log.debug("Error in getInputData", error);

      return [];

    }

     

  }




  function map(context) {

    try {

      var recordType = context.value;

      var searchObj = search.create({

        type: recordType,

        filters: [

          ["file.internalidnumber","isnotempty",""]

        ],

        columns: [

          search.createColumn({name: "internalid", label: "Internal ID"}),

          search.createColumn({name: "name", join: "file", label: "Name"}),

          search.createColumn({name: "filetype", join: "file", label: "Type"}),

          search.createColumn({name: "documentsize", join: "file", label: "Size (KB)"})

        ]

      });

      var pagedData = searchObj.runPaged({pageSize: 1000});

      pagedData.pageRanges.forEach(function(pageRange) {

        var page = pagedData.fetch({index: pageRange.index});

        page.data.forEach(function(result) {

          context.write({

            key: recordType,

            value: {

              count: 1,

              size: result.getValue({name: "documentsize", join: "file", label: "Size (KB)"})

            }

          });

        });

      }); 

    } catch (error) {

      log.debug("Error in map", error);

    }

  }




  function reduce(context) {

    try {

      var recordType = context.key;

      var totalSize = 0;

      var totalCount = 0;

      context.values.forEach(function(value) {

        var parsedValue = JSON.parse(value);

        totalCount += parsedValue.count;

        totalSize += parsedValue.size;

      });

      context.write(recordType, {count: totalCount, size: totalSize});

    } catch (error) {

      log.debug("Error in reduce", error);

    }

  }




  function summarize(summary) {

    try {

      const FOLDER_ID = '57274392';

      var recordsWithAttachments = [];

      summary.output.iterator().each(function (key, value) {

        var parsedValue = JSON.parse(value);

        recordsWithAttachments.push(key + ',' + parsedValue.count);

        return true;

      });

      var fileObj = file.create({

        name: 'records_with_attachments'+ new Date().toLocaleDateString() + '.csv',

        fileType: file.Type.CSV,

        contents: 'Record Type,Count,Size (KB)n' + recordsWithAttachments.join('n'),

        folder: FOLDER_ID

      });

      var fileId = fileObj.save();

    } catch (error) {

      log.debug("Error in summarize", error);

    }

  }




  return {

    getInputData: getInputData,

    map: map,

    reduce: reduce,

    summarize: summarize

  };

});

Leave a comment

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