【问题标题】:NetSuite SuiteScript 2.0 Cannot programmatically enter Inventory Details sublist subrecord for partially created current recordNetSuite SuiteScript 2.0 无法以编程方式为部分创建的当前记录输入库存详细信息子列表子记录
【发布时间】:2019-05-26 06:49:09
【问题描述】:

我正在尝试使用以下代码在客户端脚本中通过 SuiteScript 2.0 以编程方式(自动)创建库存详细信息字段。库存调整表单上的项目行将被部分输入(直到调整数量字段),此时我想自动添加库存详细信息。

但是代码错误就行了

inventoryDetailSubrecord = currentRecord.getSubrecord({
                           fieldId: 'inventorydetail'
                        });

带有错误消息SSS_INVALID_FIELD_ON_SUBRECORD_OPERATION。我不知道它为什么会引发这个错误,因为fieldId 看起来肯定是有效的。几乎没有关于此错误的信息。有谁知道我哪里出错了?

/**
 * @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.
         var inventoryDetailSubrecord;   // Used to access the Inventory Detail Icon fields.

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

                  // If the first item is negative then we have to increment the lot number.
                  if (firstItemNegative) {

                     // Get the inventory detail subrecord of the current line.
                     inventoryDetailSubrecord = currentRecord.getCurrentSublistSubrecord({
                        sublistId: 'inventory',
                        fieldId: 'inventorydetail'
                     });

                     // If the inventory detail subrecord does not exist, then create one.
                     if (!inventoryDetailSubrecord) {

                        // Errors with SSS_INVALID_FIELD_ON_SUBRECORD_OPERATION.
                        inventoryDetailSubrecord = currentRecord.getSubrecord({
                           fieldId: 'inventorydetail'
                        });

                        // Select a new inventory detail subrecord line.
                        inventoryDetailSubrecord.selectNewLine({
                           sublistId: 'inventory'
                        });

                        // Set the lot number.
                        inventoryDetailSubrecord.setCurrentSublistValue({
                           sublistId: 'inventory',
                           fieldId: 'issueinventorynumber',
                           value: '1'
                        });

                        // Set the quantity.
                        inventoryDetailSubrecord.setCurrentSublistValue({
                           sublistId: 'inventory',
                           fieldId: 'quantity',
                           value: quantity
                        });

                        // Commit the sublist.
                        objRecord.commitLine({
                           sublistId: 'inventory'
                        });

                     }   // if (!inventoryDetailSubrecord)

                  }   // if (firstItemNegative)

               } 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

【问题讨论】:

    标签: netsuite suitescript2.0


    【解决方案1】:

    因为无法通过客户端脚本创建库存明细子记录。 以编程方式创建库存详细信息的唯一方法是使用服务器脚本创建整个父记录。

    【讨论】:

      【解决方案2】:

      无法使用客户端脚本创建子记录。您需要使用用户事件脚本,或者,可能从您的客户端脚本发布到 Suitelet。

      该错误是因为NetSuite在使用getSubrecord时尝试创建不存在的记录。

      来自文档:

      客户端脚本可能不会在当前记录上创建子记录,并且仅限于对当前记录上的现有子记录进行只读访问。客户端脚本可能会从当前记录中删除子记录。

      【讨论】:

      • 您能否说明您在帮助中找到此内容的位置。这是否意味着我也无法读取部分输入的子记录?如果我只想验证是否正确输入了子记录(批号必须从第一行的编号递增),我将如何在客户端脚本中执行此操作?因为帮助中确实说明了可以从客户端脚本调用 CurrentRecord.getCurrentSublistSubrecord 之类的函数。
      • Supported Deployments for Subrecord Scripting 下。如果记录不存在,您可以捕获错误。读书很好。
      【解决方案3】:

      你可以试试这个代码吗?

      inventoryDetailSubrecord = currentRecord.getSublistSubrecord({
          sublistId: 'item',
          fieldId: 'inventorydetail',
          line: line#
      });
      

      【讨论】:

      • 给我这个错误:TypeError currentRecord.getSublistSubrecord is not a function
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 2017-09-17
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      相关资源
      最近更新 更多