Hello, I want to ask regarding item Serial Number ...
# suitescript
m
Hello, I want to ask regarding item Serial Number on item receipt. I try creating custom transaction from item receipt. I have successfully get other data from item receipt and put into this custom transaction, but when i try get the serial number, is said error with this message : "The following serial number is already in inventory: 200000". I just want to make notes not creating new inventory, is there any way to do that? thank you. here's my snippet code
Copy code
// Serial Numbers
var serialNumbers = [];
for (var j = 0; j < quantity; j++) {
var serial = itemReceiptRecord.getSublistValue({
             sublistId: "item",
             fieldId: "serialnumbers",
             line: i,
            });
            serialNumbers.push(" " + serial);
          }
var serialNumbersString = serialNumbers.join("\n");
b
not enough code shared
you appear to be looping using unknown index i
though that is unlikely to throw your error
m
Sorry for the confusion here is my full code
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */

define(["N/record", "N/log", "N/redirect"], function (record, log, redirect) {
  function onRequest(context) {
    if (context.request.method === "GET") {
      // Get the Item Receipt ID from the request parameters
      var itemReceiptId = context.request.parameters.itemReceiptId;

      if (!itemReceiptId) {
        // If Item Receipt ID is not provided, display an error message
        context.response.write("Item Receipt ID is required.");
        return;
      }

      try {
        // Load the Item Receipt record
        var itemReceiptRecord = record.load({
          type: record.Type.ITEM_RECEIPT,
          id: itemReceiptId,
          isDynamic: true,
        });

        // Create new custom transaction record
        var newTransaction = record.create({
          type: "custompurchase128", // Change to the desired transaction type
          isDynamic: true,
        });

        var entity = itemReceiptRecord.getValue({
          fieldId: "entity",
        });
        newTransaction.setValue({
          fieldId: "entity",
          value: entity,
        });

        // Loop through item lines on the item receipt and add them to the new transaction
        var itemCount = itemReceiptRecord.getLineCount({
          sublistId: "item",
        });
        for (var i = 0; i < itemCount; i++) {
          // Item
          var item = itemReceiptRecord.getSublistValue({
            sublistId: "item",
            fieldId: "item",
            line: i,
          });
          // Vendor
          var vendor = itemReceiptRecord.getSublistValue({
            sublistId: "item",
            fieldId: "entity",
            line: i,
          });

          // Description
          var description = itemReceiptRecord.getSublistValue({
            sublistId: "item",
            fieldId: "description",
            line: i,
          });

          // Location / Site
          var location = itemReceiptRecord.getSublistValue({
            sublistId: "item",
            fieldId: "location",
            line: i,
          });

          // Quantity
          var quantity = itemReceiptRecord.getSublistValue({
            sublistId: "item",
            fieldId: "quantity",
            line: i,
          });

          // Units
          var units = itemReceiptRecord.getSublistValue({
            sublistId: "item",
            fieldId: "units",
            line: i,
          });

          // Options
          var options = itemReceiptRecord.getSublistValue({
            sublistId: "item",
            fieldId: "options",
            line: i,
          });

          // Serial Numbers
          // var serialNumbers = [];
          // for (var j = 0; j < quantity; j++) {
          //   var serial = itemReceiptRecord.getSublistValue({
          //     sublistId: "item",
          //     fieldId: "serialnumbers",
          //     line: i,
          //   });
          //   serialNumbers.push(serial);
          // }
          var serialNumbers = itemReceiptRecord.getSublistValue({
            sublistId: "item",
            fieldId: "serialnumbers",
            line: i,
          });

          log.debug({
            title: "Serial Numbers",
            details: serialNumbers,
          });

          // Add item lines to the new transaction
          newTransaction.selectNewLine({
            sublistId: "item",
          });
          newTransaction.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "item",
            value: item,
          });
          newTransaction.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "entity",
            value: vendor,
          });
          newTransaction.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "description",
            value: description,
          });
          newTransaction.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "location",
            value: location,
          });
          newTransaction.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "quantity",
            value: quantity,
          });
          newTransaction.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "units",
            value: units,
          });
          newTransaction.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "options",
            value: options,
          });
          newTransaction.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "serialnumbers",
            value: serial,
          });
          newTransaction.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "rate",
            value: 0,
          });

          // for (var k = 0; k < serialNumbers.length; k++) {
          //   newTransaction.selectNewLine({
          //     sublistId: "item",
          //   });
          //   // Set other sublist fields
          //   newTransaction.setCurrentSublistValue({
          //     sublistId: "item",
          //     fieldId: "serialnumbers",
          //     value: serialNumbers[k], // Use each serial number individually
          //   });
          // }
          newTransaction.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "serialnumbers",
            value: serialNumbers,
          });

          newTransaction.commitLine({
            sublistId: "item",
          });
        }

        // Save the new transaction
        var transactionId = newTransaction.save();

        // Redirect to the created transaction
        redirect.toRecord({
          type: "custompurchase128",
          id: transactionId,
          isEditMode: true,
        });
      } catch (e) {
        // Handle errors
        log.error("Error Creating Transaction", e.toString());
        context.response.write("Error creating transaction. Please check logs for details.");
      }
    }
  }

  return {
    onRequest: onRequest,
  };
});
b
still nothing shared that would get you your error
you would get an undefined error for your serial variable before anything interesting happens
c
@Muhammad Hafiz I don't know anything about custom transaction types, but it might be that if you've defined this as a Purchase transaction style, it's creating an inbound inventory impact equivalent to receiving the same serialized item again.
m
Thank you for the insight, I will try another way