/** * Module Description * * Version Date Author Remarks * 1.00 29 Nov 2019 Nirunika * */ /** * @NApiVersion 2.x * @NScriptType MapReduceScript * @NModuleScope SameAccount */ define(['N/search', 'N/record', 'N/email', 'N/runtime', 'N/file'], function (search, record, email, runtime, file) { /** * Marks the beginning of the Map/Reduce process and generates input data. * * @typedef {Object} ObjectRef * @property {number} id - Internal ID of the record instance * @property {string} type - Record type id * * @return {Array|Object|Search|RecordRef} inputSummary * @since 2015.1 */ function getInputData() { try { var scriptObj = runtime.getCurrentScript(); var soAdjId = scriptObj.getParameter( { name: 'custscript_sws_so_adjustment_rec' }); var fileId = scriptObj.getParameter( { name: 'custscript_sws_partial_shipment_file' }); var notifyEmail = scriptObj.getParameter( { name: 'custscript_sws_notify_email' }); var fileDataJson = []; var firstRow = new Array(); var fileData = {}; var fileDataFilter = new Array(); //Load File var dsiFileObj = file.load( { id: fileId }); if (dsiFileObj.size > 0) { //log.debug("SOAdjustment", "dsiPartialShipFile Content= " + dsiFileObj.getContents()); log.debug("SOAdjustment", 'File processing... ', fileId); var arrLines = dsiFileObj.getContents().split(/\n|\n\r/); for (var x = 0; x < arrLines.length; x++) //iteration of csv data { var content = arrLines[x].split(','); if (x == 0) //header { for (var y = 0; y < content.length; y++) { var temp = content[y]; if (temp.indexOf('\r')) temp = temp.replace('\r', ''); firstRow.push(temp); } } else //data { var dataRows = new Array(); for (var i = 0; i < content.length; i++) { var temp1 = content[i]; if (temp1.indexOf('\r')) temp1 = temp1.replace('\r', ''); dataRows.push(temp1); } if (dataRows[1] && dataRows[2] && dataRows[3]) //if SO, item and uom { //array of json fileData = { subDiv: (dataRows[0]), soNum: (dataRows[1]), itemNum: (dataRows[2]), uom: (dataRows[3]), lineSt: (dataRows[4]), cusNum: (dataRows[5]), cusName: (dataRows[6]), shipCode: (dataRows[7]), acceptBck: (dataRows[8]), qty: (dataRows[9]), }; //Push file data fileDataJson.push(fileData); log.debug("SOAdjustment", "fileDataJson = " + JSON.stringify(fileDataJson)); var tempFilter = [ ["formulatext: {unit}", "contains", fileData.uom], "AND", ["number", "equalto", fileData.soNum], "AND", ["formulatext: {item}", "contains", fileData.itemNum] ]; //log.debug("SOAdjustment", "tempFilter= " + tempFilter); fileDataFilter.push(tempFilter); fileDataFilter.push('OR'); } } } firstRow.push("STATUS"); log.debug("SOAdjustment", "Header firstRow= " + firstRow); if (fileDataFilter.length > 1) fileDataFilter.pop(); //log.debug("SOAdjustment", "fileDataFilter= " + fileDataFilter); //Search to filter the SO's from the list var soSearchObj = search.create( { type: "salesorder", filters: [ ["type", "anyof", "SalesOrd"], "AND", ["quantitycommitted", "greaterthan", "0"], "AND", ["mainline", "is", "F"], "AND", fileDataFilter ], columns: [ search.createColumn( { name: "internalid", sort: search.Sort.ASC, label: "Internal ID" }), search.createColumn( { name: "lineuniquekey", label: "Line Unique Key" }), search.createColumn( { name: "formulatext", formula: "case when instr({item.itemid},' : ')!=0 then substr({item.itemid},instr({item.itemid},' : ')+3) else {item.itemid} end", label: "Item Number" }), search.createColumn( { name: "tranid", label: "Document Number" }), search.createColumn( { name: "formulatext", formula: "{unit}", label: "Unit" }), search.createColumn( { name: "itemid", join: "item", label: "Name" }), ] }); //log.debug("SOAdjustment", "SO search"); log.audit("SOAdjustment", JSON.stringify(soSearchObj.filterExpression)); //[json1 == json2: 'status' ->json1.sattus] var status; var prssdJsonArr = []; var proJson = {}; proJson.status = ''; var soResultCount = soSearchObj.runPaged().count; log.debug("SOAdjustment", "salesorderSearchObj result count = " + soResultCount); //return soSearchObj; soSearchObj.run().each(function (result) { // .run().each has a limit of 4,000 results var soRecId = result.getValue('internalid'); var lineUnqKey = result.getValue('lineuniquekey'); var itemNumber = result.getValue( { name: "formulatext", formula: "case when instr({item.itemid},' : ')!=0 then substr({item.itemid},instr({item.itemid},' : ')+3) else {item.itemid} end", label: "Formula (Text)" }); var docNum = result.getValue('tranid'); var units = result.getValue( { name: "formulatext", formula: "{unit}", label: "Formula (Text)" }); log.debug("SOAdjustment", "soRecId= " + soRecId + " | lineUnqKey= " + lineUnqKey + " | itemNumber= " + itemNumber + " | order# = " + docNum + " | units= " + units); return true; }); } } catch (e) { log.debug("ERROR getInputData", "Unexpected Error: " + e.toString()); } } /** * Executes when the map entry point is triggered and applies to each key/value pair. * * @param {MapSummary} context - Data collection containing the key/value pairs to process through the map stage * @since 2015.1 */ function map(mapContext) { try { var searchString = JSON.stringify(mapContext.value); var searchResult = JSON.parse(mapContext.value); var soRecId = mapContext.key; log.debug("MAP SOAdjustment", "searchString =" + searchString + " | soRecId= " + soRecId); //case if(x:)remove x: =>item else item=item; var itemId = searchResult.values.itemid; var lineUnqKey = searchResult.values.lineuniquekey; var units = searchResult.values.formulatext; var docNum = searchResult.values.tranid; log.debug("MAP SOAdjustment", "lineUnqKey =" + lineUnqKey + " | units= " + units + " | docNum= " + docNum + " | itemId= " + itemId); var objValues = { 'itemId': itemId, 'lineUnqKey': lineUnqKey, 'units': units, 'docNum': docNum }; mapContext.write( { key: soRecId, value: objValues }); } catch (e) { log.debug("ERROR Map", "Unexpected Error: " + e.toString()); } } /** * Executes when the reduce entry point is triggered and applies to each group. * * @param {ReduceSummary} context - Data collection containing the groups to process through the reduce stage * @since 2015.1 */ function reduce(reduceContext) { var reduceString1 = JSON.stringify(reduceContext); log.debug( { title: 'reduce', details: 'reduceString1 = ' + reduceString1 }); var soRecId = reduceContext.key; var arrValues = reduceContext.values; log.audit( { title: 'reduce', details: ' soRecId = ' + soRecId + ' | arrValues = ' + arrValues.length }); } /** * Executes when the summarize entry point is triggered and applies to the result set. * * @param {Summary} summary - Holds statistics regarding the execution of a map/reduce script * @since 2015.1 */ function summarize(summary) {} return { getInputData: getInputData, map: map, reduce: reduce, summarize: summarize }; });