Goods Receiving – send conformation using Script

This script is used for sending a conformation of received items from Purchase order. The script creates a file for the received item receipt and the global system connector fetch this file from the file cabinet to an external system.

The script is a scheduled script which runs in every hour daily. When a file is generated for item receipt, corresponding receipt record will be flagged received.

     // ?X?P?W??c?[???p??????[?^?????g?
     var folderId = nlapiGetContext().getSetting('SCRIPT', 'custscript_folder_id_realtime');
     var filePrefix = nlapiGetContext().getSetting('SCRIPT', 'custscript_file_prefix_realtime');
     var headerFlg = nlapiGetContext().getSetting('SCRIPT', 'custscript_header_flg_realtime');
     var separator = nlapiGetContext().getSetting('SCRIPT', 'custscript_separator_realtime');
     var searchIdAsndata = nlapiGetContext().getSetting('SCRIPT', 'custscript_search_id_realtime_asndata');
     // var searchIdKitdata = nlapiGetContext().getSetting('SCRIPT',
     // 'custscript_search_id_realtime_kitdata');
     // var searchIdPolodata = nlapiGetContext().getSetting('SCRIPT',
     // 'custscript_search_id_realtime_polodata');
     // CSV?t?@?C???G??g?R?[?h
     var encoding = 'UTF-8';
     // ?f?[?^???X?g
     var fileLines = [];

     /**
      * ???C??g???\?b?h
      *
      * @returns
      */
     function main() {

         try {

             // ASN?f?[?^
             var curInvoice = getItems(searchIdAsndata, headerFlg);

             curInvoice.forEach(function(soLine) {

                 nlapiLogExecution('DEBUG', 'CsoLine', soLine);
                 nlapiLogExecution('DEBUG', 'CsoLine', soLine[11]);

                 var soLineArray = JSON.stringify(soLine).split(",");
                 var conversionObj = checkifFromUOMtable(soLineArray[7], soLineArray[12], soLineArray[11]);
                 if (conversionObj == false) {
                     fileLines.push(makeLine(soLine, separator));
                 } else {
                     soLine[12] = conversionObj.unit;
                     soLine[11] = parseFloat(conversionObj.conversionQty).toString();
                     //  var soLine = soLineArray.toString().replace(/"/g, "").replace('[', "").replace("]", "");
                     nlapiLogExecution('DEBUG', 'soLine', soLine);
                     fileLines.push(makeLine(soLine, separator));

                 }
             });

             /*
              * // KIT?f?[?^ var curReturn = getItems(searchIdKitdata, headerFlg);
              * curReturn.forEach(function(soLine) { fileLines.push(makeLine(soLine,
              * separator)); });
              *  // POLO?f?[?^ var curReturn = getItems(searchIdPolodata, headerFlg);
              * curReturn.forEach(function(soLine) { fileLines.push(makeLine(soLine,
              * separator)); });
              */
             if (fileLines.length > 0) {
                 fileLines.push("");
             } else {
                 return;
             }

             // CSV?t?@?C????
             var fileN = getTxtDLFileName();
             var fileName = (filePrefix + fileN);
             var file = nlapiCreateFile(fileName, 'PLAINTEXT', fileLines.join('\r\n'));

             // CSV?t?@?C???????
             file.setFolder(folderId);
             file.setName(fileName);
             file.setEncoding(encoding);
             if (file.getSize() > 0) {
                 var fileId = nlapiSubmitFile(file);
                 nlapiLogExecution('DEBUG', 'CSV File Id:' + fileId);
             }
         } catch (e) {
             throw nlapiCreateError('Failed to CSV:', e);
         }
     }

     /**
      * ???e???????????f?[?^?????g?????
      *
      * @returns {searchResults} ???e???????????
      */
     function getItems(savedSearchId, hdFlg) {

         // ????h?f??`
         var searchResults = [];
         var searchCount = 0;

         // ???e??????ID?`?F?b?N
         if (savedSearchId == null || savedSearchId == '') {
             return searchResults;
         }

         // ???e?????????????o???
         //  var savedSearch = nlapiLoadSearch(null, savedSearchId);

         // ???e???????????s
         // var resultSet = savedSearch.runSearch();

         do {

             //  var resultSlice = resultSet.getResults(searchCount, searchCount + 1000);
             /*Updated by Edwin on 03-12-2018 */
             /*Instead of Loading the search we are creating the saved search*/
             var resultSlice = nlapiSearchRecord("itemreceipt", null,
                 [
                     ["type", "anyof", "ItemRcpt"],
                     "AND",
                     ["mainline", "is", "F"],
                     "AND",
                     ["taxline", "is", "F"],
                     "AND",
                     ["formulatext: {item.custitem_sw_global_item_code}", "isnotempty", ""],
                     "AND",
                     ["quantity", "isnotempty", ""],
                     "AND",
                     ["datecreated", "onorbefore", "today"],
                     "AND",
                     ["custbody_sw_gr_processed_flag", "is", "F"],
                     "AND",
                     ["inventorydetail.status", "noneof", "@NONE@"],
                     "AND",
                     ["createdfrom.type", "anyof", "PurchOrd"]
                 ],
                 [
                     new nlobjSearchColumn("formulatext").setFormula("'D1JP'"),
                     new nlobjSearchColumn("formulatext").setFormula("'0010'"),
                     new nlobjSearchColumn("custcol_sw_po_line_no"),
                     new nlobjSearchColumn("formulatext").setFormula("''"),
                     new nlobjSearchColumn("formulatext").setFormula("''"),
                     new nlobjSearchColumn("custbody_prb_asn_purchase_order_num", "createdFrom", null),
                     new nlobjSearchColumn("custcol_prb_asn_line_item_num"),
                     new nlobjSearchColumn("custitem_sw_global_item_code", "item", null),
                     new nlobjSearchColumn("inventorynumber", "inventoryDetail", null),
                     new nlobjSearchColumn("formulatext").setFormula("''"),
                     new nlobjSearchColumn("formulatext").setFormula("''"),
                     new nlobjSearchColumn("formulanumeric").setFormula("ABS({inventorydetail.quantity}/({quantity}/{quantityuom}))"),
                     new nlobjSearchColumn("unit"),
                     new nlobjSearchColumn("custcol_prb_cust_purchase_order_num"),
                     new nlobjSearchColumn("formulatext").setFormula("''"),
                     new nlobjSearchColumn("formulatext").setFormula("''"),
                     new nlobjSearchColumn("formulatext").setFormula("''"),
                     new nlobjSearchColumn("formulatext").setFormula("''"),
                     new nlobjSearchColumn("formulatext").setFormula("TO_CHAR({trandate}, 'YYYYMMDD')")
                 ]);
             //change processedFlag of itemReceipt
             changeRecordFlag(resultSlice);
             // ?K?o?i??g?X???`?F?b?N
             governanceYield();

             if (resultSlice && resultSlice.length > 0) {

                 // ?w?b?_?[??????
                 if (hdFlg == "true") {
                     if (fileLines.length == 0) {
                         fileLines.push(makeHeader(resultSlice[0], separator));
                     }
                 }

                 // ?f?[?^???\?
                 for (var rs in resultSlice) {
                     // ?K?o?i??g?X???`?F?b?N
                     governanceYield();
                     // CSV?f?[?^?????\?????
                     var returnLine = [];
                     var colum = resultSlice[rs].getAllColumns();
                     for (var a in colum) {
                         returnLine.push(resultSlice[rs].getText(colum[a]) || resultSlice[rs].getValue(colum[a]))
                     }
                     // ?f?[?^???e??
                     searchResults.push(returnLine);
                     searchCount++;
                     // ?K?o?i??g?X???`?F?b?N
                     governanceYield();
                 }
             }

             // ?K?o?i??g?X???`?F?b?N
             governanceYield();
         } while (resultSlice && resultSlice.length >= 1000);

         // ?????fl
         return searchResults;
     }
     /**
      * change processed_flag of itemReceipt
      *
      * @param results
      */
     function changeRecordFlag(results) {
         if (!results || results.length < 1)
             return;
         var idArray = new Array();
         for (var i = 0; i < results.length; i++) {
             var id = results[i].getId();
             if (idArray.indexOf(id) < 0) {
                 governanceYield();
                 nlapiSubmitField("itemreceipt", id, "custbody_sw_gr_processed_flag", "T");
                 idArray.push(id);
             }
         }
     }

     function checkifFromUOMtable(item, unitName, quantity) {
         try {

             var quantity = parseFloat(quantity.replace(/"/g, ""));
             nlapiLogExecution('DEBUG', ' item unitName quantity', item.replace(/"/g, "") + "  " + unitName + "  " + quantity);
             var internalId = getLocalid(item.replace(/"/g, ""));
             nlapiLogExecution("DEBUG", "internalid", internalId);

             var customrecord_sw_order_uon_conversionSearch = nlapiSearchRecord("customrecord_sw_order_uon_conversion", null,
                 [
                     ["custrecord_sw_item_uom_conversion_table.internalidnumber", "equalto", internalId]
                 ],
                 [
                     new nlobjSearchColumn("scriptid").setSort(false),
                     new nlobjSearchColumn("custrecord_sw_item_uom_conversion_table"),
                     new nlobjSearchColumn("custrecordsw_asn_uom_conversion_table"),
                     new nlobjSearchColumn("custrecordsw_po_uom_conversion_table"),
                     new nlobjSearchColumn("custrecord_sw_conve_uom_conversion_table"),
                     new nlobjSearchColumn("custrecord_sw_order_mult_conversion")

                 ]
             );

             if (customrecord_sw_order_uon_conversionSearch != null) {
                 var asnOrdeuom = customrecord_sw_order_uon_conversionSearch[0].getText("custrecordsw_asn_uom_conversion_table");
                 var poOrdeuom = customrecord_sw_order_uon_conversionSearch[0].getText("custrecordsw_po_uom_conversion_table");
                 var Division_conversionUnit = customrecord_sw_order_uon_conversionSearch[0].getValue("custrecord_sw_conve_uom_conversion_table");
                 var Multiple_Conversion_unit = customrecord_sw_order_uon_conversionSearch[0].getValue("custrecord_sw_order_mult_conversion");

                 nlapiLogExecution('DEBUG', 'asnOrdeuom', asnOrdeuom);
                 nlapiLogExecution('DEBUG', 'poOrdeuom', poOrdeuom);
                 nlapiLogExecution('DEBUG', 'Division_conversionUnit', Division_conversionUnit);
                 nlapiLogExecution('DEBUG', 'Multiple_Conversion_unit', Multiple_Conversion_unit);

                 if (poOrdeuom == unitName.replace(/"/g, "")) {
                     if (Division_conversionUnit >= 1) {
                         nlapiLogExecution("DEBUG", "DIVISION", Division_conversionUnit)
                         return {
                             unit: asnOrdeuom,
                             conversionQty: quantity * parseFloat(Division_conversionUnit)
                         }
                     } else {
                         nlapiLogExecution("DEBUG", "MULTIPLICATION", Multiple_Conversion_unit)

                         return {
                             unit: asnOrdeuom,
                             conversionQty: quantity / parseFloat(Multiple_Conversion_unit)
                         }

                     }
                 } else {

                     return false
                 }
             } else {
                 return false

             }
         } catch (e) {
             nlapiLogExecution('DEBUG', 'Error in checkifFromUOMtable ', e);
         }

     }





     function getLocalid(globalname) {
         try {

             nlapiLogExecution("DEBUG", "globalname", globalname);

             var inventoryitemSearch = nlapiSearchRecord("inventoryitem", null,
                 [
                     ["type", "anyof", "InvtPart"],
                     "AND",
                     ["custitem_sw_global_item_code.name", "is", globalname]
                 ],
                 [
                     new nlobjSearchColumn("internalid")
                 ]
             );
             return inventoryitemSearch[0].getValue("internalid")
         } catch (e) {
             nlapiLogExecution("DEBUG", "e", e);
         }
     }

Leave a comment

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