Netsuite SuiteScript 2.0如何遍历所有订单项和总数量

时间:2018-12-27 09:08:05

标签: netsuite sublist suitescript2.0

我试图遍历Netsuite SuiteScript 2.0中“库存调整”屏幕的所有行项目并总计数量(按数量调整)字段,以检查其总和为零(第一行必须保持负值)其他正线必须匹配)。不幸的是,我对此并不陌生,并且也找不到如何执行此操作的任何示例。我还需要最终更新批号,以便从第一行的编号开始为每行递增。所有行还必须具有相同的父项。

在执行此操作时,我遇到了很多问题,即多次触发数量字段更改事件,警报将在用户总数达到零的途中使用户感到烦恼,只有当屏幕底部的total是0,依此类推。我当前的问题是,即使我在各行之间循环,我的代码也只是对正在输入的行(当前行)求和。我尝试使用getSublistValue代替getCurrentSublistValue,但这给了我一条错误消息。我尝试使用selectLine,但这试图在仍在输入该行并且保留必填字段的同时移动当前活动的行。我找不到getSublistFields的任何示例代码来潜在地获取所有值。

我们正在使用Netsuite 2018.2,但我缺少SQL。

如何在Netsuite SuiteScript 2.0中完成此操作,我是否可以同时更新子列表子记录(“清单详细信息”图标中的批次编号)?

/**
 * @NApiVersion 2.0
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define([], function () {

   // Client Script global variables.
   var allowSave = true;
   var firstItemNegative = false;
   var firstItemParentID = -9999;

   function fieldChanged(context) {

      var currentRecord = context.currentRecord;    // Current opened record.
      var sublistName = context.sublistId;          // The internal ID of the sublist.
      var sublistFieldName = context.fieldId;       // The internal ID of the field that was changed.
      var currentLine = context.line;               // Line number (first line has value = 0) of Item User is on.

      // Run when the Item field of the inventory sublist changed.
      // Item for some reason does not fire a change event, so using item description instead.
      // This means the description has to be required for these types of items.
      if (sublistName === 'inventory' && sublistFieldName === 'description') {

         // Check how many lines exist in the inventory sublist.
         var lines = currentRecord.getLineCount({sublistId: 'inventory'});
         // console.info("SS lines: " + lines);

      // Run when the Adjust Qty. By field of the inventory sublist changed.
      } else if (sublistName === 'inventory' && sublistFieldName === 'adjustqtyby') {

         console.info("SS fieldChanged: " + (context.sublistId || "record") + "." + context.fieldId);

         // Check how many lines exist in the inventory sublist.
         var lines = currentRecord.getLineCount({sublistId: 'inventory'});
         console.info("SS lines: " + lines);

         var total;      // Total used to check whether sum of quantities is zero.
         var quantity;   // Used to hold quantity for current line item.

         for (var i = 0; i < lines + 1; i++) {
console.info("SS total start: " + total);
            currentRecord.selectLine({
               sublistId: "inventory",
               line: i
            });
            // If we are on the first item line.
            if (i === 0) {
               // Get the first item line's Adjust Qty. By field value.
               total = parseFloat(currentRecord.getCurrentSublistValue({
                  sublistId: "inventory",
                  fieldId: "adjustqtyby",
                  line: i
               }));
console.info("SS total first line: " + total);
               // If the quantity of the first line is positive then this is a real Inventory Adjustment
               // and not a roll that was cut into smaller inventory.
               if (total >= 0) {
                  firstItemNegative = false;
               } else {
                  firstItemNegative = true;
               }
            } else if (i > 0) {   // For non-first lines.
               // Get the current item line's Adjust Qty. By field value.
               // Note that the value could be invalid in which case 0 is used.
               quantity = (parseFloat(currentRecord.getCurrentSublistValue({
                  sublistId: "inventory",
                  fieldId: "adjustqtyby",
                  line: i
               })) || 0);
console.info("SS quantity: " + quantity);
               // If the first item is negative then we have to keep a running total of the quantities.
               if (firstItemNegative) {
                  total = total + quantity;
console.info("SS total other lines: " + total);
               } else {   // If the first item is positive we have to check that there are no other negative quantities.
                  if (quantity < 0) {
                     allowSave = false;
                     // Show modeless Netsuite banner message at top of screen that is replaced by subsequent messages.
                     // If you use the same id in the first parameter it will overwrite the message, if you supply a different id you will see new messages uniquely in the page.
                     showAlertBox(
                        "my_element_id",   // Dummy element id of alert.
                        "Error:",          // Message header.
                        'Inventory Item line number ' + (i + 1) + ' has a negative "Adjust Qty. By" field value. Negative values are only allowed for the first item.',
                        3,                 // Colour of alert: 0 - Success (green), 1 - Information (blue), 2 - Warning (yellow), 3 - Error (red)
                        "","","",""        // Not sure what this does.
                     );
                     break;
                  }
               }
            }   // if (i === 0)

         }   // for (var i = 0; i < lines + 1; i++)
console.info("SS total end: " + total);
         // If the total of the quantities are not zero then error. Allow if only the first line exists.
         if (total !== 0 && lines !== 0) {
            allowSave = false;
            if (total < 0) {
               showAlertBox(
                  "my_element_id",   // Dummy element id of alert.
                  "Error:",          // Message header.
                  'Error: The total of the "Adjust Qty. By" fields must equal zero. You are under by ' + (-total),
                  3,                 // Colour of alert: 0 - Success (green), 1 - Information (blue), 2 - Warning (yellow), 3 - Error (red)
                  "","","",""        // Not sure what this does.
               );
            } else {
               showAlertBox(
                  "my_element_id",   // Dummy element id of alert.
                  "Error:",          // Message header.
                  'Error: The total of the "Adjust Qty. By" fields must equal zero. You are over by ' + total,
                  3,                 // Colour of alert: 0 - Success (green), 1 - Information (blue), 2 - Warning (yellow), 3 - Error (red)
                  "","","",""        // Not sure what this does.
               );
            }
         } else {
            allowSave = true;
         }

      }   //  if (sublistName === 'inventory' && sublistFieldName === 'description')

      // Clear any error messages to show that all fields validated.
      if (allowSave) {
         showAlertBox(
            "my_element_id",   // Dummy element id of alert.
            "Success:",          // Message header.
            'Validation passed.',
            0,                 // Colour of alert: 0 - Success (green), 1 - Information (blue), 2 - Warning (yellow), 3 - Error (red)
            "","","",""        // Not sure what this does.
         );
      }

   }   // fieldChanged

   function saveRecord() {
      // debugger;
      console.info("SS saveRecord");
      if (!allowSave) {
         alert("Error: Save failed. There are error messages at the top of the page.");
      }
      return allowSave;
   }   // saveRecord

   return {
      fieldChanged: fieldChanged,
      saveRecord: saveRecord
   };

});   // Define

1 个答案:

答案 0 :(得分:1)

感谢K解决了这一问题,已经奋斗了几天。现在可以使用以下代码:

/**
 * @NApiVersion 2.0
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/search'], function (s) {

   // Client Script global variables.
   var allowSave = true;
   var firstItemNegative = false;
   var firstItemParentID = -9999;

   function fieldChanged(context) {

      var currentRecord = context.currentRecord;    // Current opened record.
      var sublistName = context.sublistId;          // The internal ID of the sublist.
      var sublistFieldName = context.fieldId;       // The internal ID of the field that was changed.
      var currentLine = context.line;               // Line number (first line has value = 0) of Item User is on.

      // Run when the Item field of the inventory sublist changed.
      // Item for some reason does not fire a change event, so using item description instead.
      // This means the description has to be required for these types of items.
      if (sublistName === 'inventory' && sublistFieldName === 'description') {

         // Check how many lines exist in the inventory sublist.
         var lines = currentRecord.getLineCount({sublistId: 'inventory'});
         // console.info("SS lines: " + lines);

         // if (currentRecord.isDynamic) {
         //    console.info("SS isDynamic: true");   // currentRecord is Dynamic.
         // } else {
         //    console.info("SS isDynamic: false");
         // }

      // Run when the Adjust Qty. By field of the inventory sublist changed.
      } else if (sublistName === 'inventory' && sublistFieldName === 'adjustqtyby') {

         console.info("SS fieldChanged: " + (context.sublistId || "record") + "." + context.fieldId);
         console.info("SS currentLine: " + currentLine);

         // Check how many lines exist in the inventory sublist.
         var lines = currentRecord.getLineCount({sublistId: 'inventory'});
         console.info("SS lines: " + lines);

         var total;      // Total used to check whether sum of quantities is zero.
         var quantity;   // Used to hold quantity for current line item.

         for (var i = 0; i <= lines; i++) {

            // If we are on the first item line.
            if (i === 0) {

               if (i === currentLine) {
                  // Get the first item line's Adjust Qty. By field value.
                  // Note that the value could be invalid in which case 0 is used.
                  // For partially entered lines.
                  total = (parseFloat(currentRecord.getCurrentSublistValue({
                     sublistId: "inventory",
                     fieldId: "adjustqtyby"
                  })) || 0);
               } else {
                  // Get the first item line's Adjust Qty. By field value.
                  // For completed lines that have been Added.
                  total = (parseFloat(currentRecord.getSublistValue({
                     sublistId: "inventory",
                     fieldId: "adjustqtyby",
                     line: i
                  })) || 0);
               }
               console.info("SS total first line: " + total);

               // If the quantity of the first line is positive then this is a real Inventory Adjustment
               // and not a roll that was cut into smaller inventory.
               if (total >= 0) {
                  firstItemNegative = false;
               } else {
                  firstItemNegative = true;
               }

            } else if (i > 0) {   // For non-first lines.

               if (i === currentLine) {
                  // Get the current item line's Adjust Qty. By field value.
                  quantity = (parseFloat(currentRecord.getCurrentSublistValue({
                     sublistId: "inventory",
                     fieldId: "adjustqtyby"
                  })) || 0);
               } else {
                  // Get the current item line's Adjust Qty. By field value.
                  quantity = (parseFloat(currentRecord.getSublistValue({
                     sublistId: "inventory",
                     fieldId: "adjustqtyby",
                     line: i
                  })) || 0);
               }
               console.info("SS quantity: " + quantity);

               // If the first item is negative then we have to keep a running total of the quantities.
               if (firstItemNegative) {
                  total = total + quantity;
                  console.info("SS total other lines: " + total);
               } else {   // If the first item is positive we have to check that there are no other negative quantities.
                  if (quantity < 0) {
                     allowSave = false;
                     // Show modeless Netsuite banner message at top of screen that is replaced by subsequent messages.
                     // If you use the same id in the first parameter it will overwrite the message, if you supply a different id you will see new messages uniquely in the page.
                     showAlertBox(
                        "my_element_id",   // Dummy element id of alert.
                        "Error:",          // Message header.
                        'Inventory Item line number ' + (i + 1) + ' has a negative "Adjust Qty. By" field value. Negative values are only allowed for the first item.',
                        3,                 // Colour of alert: 0 - Success (green), 1 - Information (blue), 2 - Warning (yellow), 3 - Error (red)
                        "","","",""        // Not sure what this does.
                     );
                     break;
                  }
               }
            }   // if (i === 0)

         }   // for (var i = 0; i < lines + 1; i++)
         console.info("SS total end: " + total);

         // If the total of the quantities are not zero then error. Allow if only the first line exists.
         if (total !== 0 && lines !== 0) {
            allowSave = false;
            if (total < 0) {
               showAlertBox(
                  "my_element_id",   // Dummy element id of alert.
                  "Error:",          // Message header.
                  'Error: The total of the "Adjust Qty. By" fields must equal zero. You are under by ' + (-total),
                  3,                 // Colour of alert: 0 - Success (green), 1 - Information (blue), 2 - Warning (yellow), 3 - Error (red)
                  "","","",""        // Not sure what this does.
               );
            } else {
               showAlertBox(
                  "my_element_id",   // Dummy element id of alert.
                  "Error:",          // Message header.
                  'Error: The total of the "Adjust Qty. By" fields must equal zero. You are over by ' + total,
                  3,                 // Colour of alert: 0 - Success (green), 1 - Information (blue), 2 - Warning (yellow), 3 - Error (red)
                  "","","",""        // Not sure what this does.
               );
            }
         } else {
            allowSave = true;
         }

      }   //  if (sublistName === 'inventory' && sublistFieldName === 'description')

      // Clear any error messages to show that all fields validated.
      if (allowSave) {
         showAlertBox(
            "my_element_id",   // Dummy element id of alert.
            "Success:",          // Message header.
            'Validation passed.',
            0,                 // Colour of alert: 0 - Success (green), 1 - Information (blue), 2 - Warning (yellow), 3 - Error (red)
            "","","",""        // Not sure what this does.
         );
      }

   }   // fieldChanged
   function saveRecord() {
      // debugger;
      console.info("SS saveRecord");
      if (!allowSave) {
         alert("Error: Save failed. There are error messages at the top of the page.");
      }
      return allowSave;
   }   // saveRecord

   return {
      fieldChanged: fieldChanged,
      saveRecord: saveRecord
   };

});   // Define