Deepan Murugan
Deepan Murugan

Reputation: 771

Is the Search Created using 'N/search' Module are able to run in the Map/Reduce Script?

I have loaded the search in getInputData Method of the Map/Reduce Script. and tried to run the search on the Map Method as well on the getInputData Method.

In the Normal Scripts like User Event,Scheduled Script ,Client Script,Suitlet Script and all we can create the search and able to run the search using run() or runPaged() method.

My Question is the search can be able to run using the run() or runPaged() Method in getInputData Method or on the Map Method of Map/Reduce Script ?.

If Means how to pass the search results column to the next stage that is Map or Reduce.

My Code:

define(['N/error', 'N/record', 'N/search', 'N/log', 'N/task', 'N/runtime', 'N/email'],
        /**
         * @param {email} email
         * @param {record} record
         * @param {runtime} runtime
         * @param {search} search
         * @param {task} task
         * @param {transaction} transaction
         */

        /* this script is used to create the search on the invoice and store the obtained search results on the object and trying to create the another search based on the values on the object */
        function(error, record, search, log, task, runtime, email) {

            function getInputData() {

                log.debug("Get Input", "Initiated");
                //Invoice Search
                var invoiceSearch = search.load({
                    id: 'customsearch_invoice_calc'
                });

                log.debug("invoiceSearch:", invoiceSearch);

              //Creating the Object for Storing Search Results

                var invoiceDetails = {};
                var invoiceId = "Id";
                var invoiceLineId = "invoiceLineId";

              //Running the Search
                var myPagedData = invoiceSearch.runPaged({
                    "pageSize": 1000
                });
                log.debug('myPagedData:', myPagedData);

                myPagedData.pageRanges.forEach(function(pageRange) {

                    // Fetch the results on the current page
                    var myPage = myPagedData.fetch({
                        index: pageRange.index
                    });
                    log.debug('myPage:', myPage);
                    // Iterate over the list of results on the current page
                    myPage.data.forEach(function(result) {

                        // Process the individual result
                        invoiceDetails[invoiceId] = result.getValue({
                            name: 'internalid'
                        });
                        invoiceDetails[invoiceLineId] = result.getValue({
                            name: 'line'
                        });
                    });
                })

                log.debug("invoiceDetails:", invoiceDetails);
                return invoiceSearch;
            }

            function map(context) {
                log.debug("Map", "Initiated");
                var searchResult = JSON.parse(context.value);


                var invoiceId = searchResult.id;
                var lineId = searchResult.values.line.value;
                log.debug("invoiceId:", invoiceId);
                log.debug("lineId:", lineId);
                comCalulation(invoiceId, invoiceId);


                context.write(invoiceId);
            }

            function commissionCalc(invoiceId, lineId) {
                log.debug("Entered:", "Commission Calc Function");
                log.debug("invoiceId - Inside Commission Calc:", invoiceId);
            }

            function reduce(context) {
                log.debug("Reduce", "Initiated");

            }



            function summarize(summary) {
                log.debug("summarize", "Initiated");

            }

thanks in advance.

Upvotes: 0

Views: 1161

Answers (1)

erictgrubaugh
erictgrubaugh

Reputation: 8902

If you just want to pass the results of a search from getInputData to map, then all you need to do is return the search object from getInputData. NetSuite will automatically execute the search and distribute the results to the map or reduce phase, depending on how you've configured your Map/Reduce Script record.

An example of this is given in NS Help on the page titled "Map/Reduce Script Type" as "Example 2"; I've reproduced part of it here:

function getInputData()
{
    // Input phase only needs to create/load and return search object
    return search.create({
        type: record.Type.INVOICE,
        filters: [['status', search.Operator.IS, 'open']],
        columns: ['entity'],
        title: 'Open Invoice Search'
    });
}

function map(context)
{
    // Parse individual search result
    var searchResult = JSON.parse(context.value);
    var invoiceId = searchResult.id;
    var entityId = searchResult.values.entity.value;

    applyLocationDiscountToInvoice(invoiceId);

    // Pass customerId:invoiceId to the reduce phase
    context.write(entityId, invoiceId);
}

You can see each individual search result will be passed along as context.value and needs to be parsed into an Object. map will be invoked once for each search result.

Upvotes: 3

Related Questions