代码之家  ›  专栏  ›  技术社区  ›  Superdooperhero

Netsuite SuiteScript 2.0如何循环浏览所有行项目并合计数量

  •  1
  • Superdooperhero  · 技术社区  · 7 年前

    我在这样做时遇到了很多问题,即数量字段更改事件会多次触发,警报会在总数为零的过程中对用户造成干扰,屏幕只能在总数为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 回复  |  直到 7 年前
        1
  •  2
  •   Superdooperhero    7 年前

    谢谢氪星解决了这个问题,已经挣扎了好几天了。下面的代码现在可以工作了:

    /**
     * @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
    
    推荐文章