首页 文章

使用'N/search'模块创建的搜索是否能够在Map / Reduce脚本中运行?

提问于
浏览
0

我已经在Map / Reduce脚本的getInputData方法中加载了搜索 . 并尝试在getInputData方法上运行Map方法上的搜索 .

在用户事件,预定脚本,客户端脚本,套装脚本等常规脚本中,我们可以创建搜索并使用run()或runPaged()方法运行搜索 .

我的问题是搜索能够使用getInputData方法中的run()或runPaged()方法或Map / Reduce脚本的Map方法运行吗?

如果表示如何将搜索结果列传递给Map或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");

            }

提前致谢 .

1 回答

  • 3

    如果您只想将搜索结果从 getInputData 传递给 map ,那么您需要做的就是从 getInputData 返回搜索对象 . NetSuite将自动执行搜索并将结果分发到 mapreduce 阶段,具体取决于您如何配置Map / Reduce Script记录 .

    在NS帮助中,在 Headers 为“Map / Reduce Script Type”的页面中给出了一个例子,作为“例2”;我在这里复制了部分内容:

    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);
    }
    

    您可以看到每个单独的搜索结果将作为 context.value 传递,并且需要解析为Object . 每个搜索结果将调用 map 一次 .

相关问题