Map/Reduce Script (deployed to Netsuite using SDF

1.Create a new project

File>new>project

2.open file cabinet folder and right click on the Suitescripts folder select,

new > SuiteScript File

From the popup window enter script type as Map/reduce script and name .

.js file

/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */
define(['N/file'],
    /**
 * @param{file} file
 */
    (file) => {
        /**
         * Defines the function that is executed at the beginning of the map/reduce process and generates the input data.
         * @param {Object} inputContext
         * @param {boolean} inputContext.isRestarted - Indicates whether the current invocation of this function is the first
         *     invocation (if true, the current invocation is not the first invocation and this function has been restarted)
         * @param {Object} inputContext.ObjectRef - Object that references the input data
         * @typedef {Object} ObjectRef
         * @property {string|number} ObjectRef.id - Internal ID of the record instance that contains the input data
         * @property {string} ObjectRef.type - Type of the record instance that contains the input data
         * @returns {Array|Object|Search|ObjectRef|File|Query} The input data to use in the map/reduce process
         * @since 2015.2
         */
        const PUNCTUATION_REGEXP = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#\$%&\(\)\*\+,\-\.\/:;<=>\?@\[\]\^_`\{\|\}~]/g;
        const getInputData = () => {
            return "the quick brown fox \njumps over the lazy dog.".split('\n');
        }
        const map = (context) => {
            log.debug(context.value.length)
            for (let i = 0; context.value && i < context.value.length; i++) {
                if (context.value[i] !== ' ' && !PUNCTUATION_REGEXP.test(context.value[i])) {
                    context.write({
                        key: context.value[i],
                        value: 1
                    });
                }
            }

        }
        const reduce = (context) => {
            context.write({
                key: context.key,
                value: context.values.length
            });

        }

        const summarize = (context) => {
          
            let text = '';
            let totalKeysSaved = 0;

            context.output.iterator().each(function (key, value) {
                text += (key + ' ' + value + '\n');
                totalKeysSaved++;
                return true;
            });
            log.debug({
                title: 'Unique number of letters used in string',
                details: totalKeysSaved
            });
            let fileObj = file.create({
                name: 'letter_count_result.txt',
                fileType: file.Type.PLAINTEXT,
                contents: text
            });
            fileObj.folder = -15;
            let fileId = fileObj.save();
            
        }

        return {getInputData, map, reduce, summarize}

    });

3. Right clickon the objects folder repeat step 2 to make an xml file for script deployment

.xml file

<mapreducescript scriptid="customscript_mrone">
    <isinactive>F</isinactive>
    <name>MR script WS</name>
    <notifyowner>T</notifyowner>
    <scriptfile>[/SuiteScripts/MapReduceScript.js]</scriptfile>
    <scriptdeployments>
        <scriptdeployment scriptid="customdeploy_mrone">
            <isdeployed>T</isdeployed>
            <loglevel>DEBUG</loglevel>
            <runasrole>ADMINISTRATOR</runasrole>
            <status>NOTSCHEDULED</status>
            <title>JJ MR from WS</title>
            <yieldaftermins>60</yieldaftermins>
        </scriptdeployment>
    </scriptdeployments>
</mapreducescript>


4.Right Click on the manifest.xml and add dependency reference to manifest
5.Right click on the project and validate project against account
6.Right click on the project and select deploy to account

Leave a comment

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