简体   繁体   中英

Script error when Transform PO to Item Receipt Error

I'm new to suitescript, I've got a schedule script to Transform a PO to Item Receipt. Which needs to create a record on Subrecord on inventorydetail sub record, I get "USER_ERROR","message":"The total inventory detail quantity must be 1." - Not sure what I have done wrong. If i use the same code for a non stock and remove the sub record insert line and coding it works

   var csvPOId = 73586;
        var csvPOLocationId = 1;
        var csvPOQty = 1;
        var csvBinId = '';
        var csvSerialLot = '';
        var csvInvQty = 1;
        //

        try {
            var itemReceiptRec = record.transform({
                fromType: record.Type.PURCHASE_ORDER,
                fromId: csvPOId,
                toType: record.Type.ITEM_RECEIPT,
                isDynamic: false
            });
        } catch (e) {
            log.error({
                title: e.name,
                details: e.message
            });
        }


        // Get count - Sublist Lines
        var count = itemReceiptRec.getLineCount({
            sublistId: 'item'
        });

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

            // Item Sub List - Mark as Received
            var itemReceived = itemReceiptRec.getSublistValue({
                sublistId: 'item',
                fieldId: 'itemreceive',
                line: i
            });

            // Location
            itemReceiptRec.setSublistValue({
                sublistId: 'item',
                fieldId: 'location',
                value: csvPOLocationId, // '11 - Central Main Warehouse',
                line: i
            });
            // Quantity

            itemReceiptRec.setSublistValue({
                sublistId: 'item',
                fieldId: 'quantity',
                value: csvPOQty,
                line: i
            });

            // Inventory Detail Sub Record [inventorydetail]

            var subrec = itemReceiptRec.getSublistSubrecord({
                sublistId: 'item',
                line: i,
                fieldId: 'inventorydetail'
            });
            subrec.insertLine({
                sublistId: 'inventoryassignment',
                line: 0
            });
            // // Quantity - Mandatory
            subrec.setSublistValue({
                sublistId: 'inventoryassignment',
                fieldId: 'quantity',
                line: 0,
                value: csvInvQty
            });
        var itemRecID = itemReceiptRec.save({
            enableSourceing: true
        });

        log.debug({
            title: 'New Item Receipt Record',
            details: 'Internal ID ' + itemRecID
        });

I guess the issue is with your way of setting the values.

Follow this logic (haven't included the subrecord logic)-

for(var j=0; j<count; j++) {

   itemReceiptRec.selectLine({
      sublistId: 'item',
      line: j
   )};

   var itemReceived = itemReceiptRec.getCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'itemreceive',
                line: j
   });

   // Location
   itemReceiptRec.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'location',
        value: csvPOLocationId, // '11 - Central Main Warehouse',
   });

   // Quantity
   itemReceiptRec.setSublistValue({
        sublistId: 'item',
        fieldId: 'quantity',
        value: csvPOQty,
   });

   //Similarly add subrecord code logic
   //Notice I haven't used 'line' while setting sublist value
 
   itemReceiptRec.commitLine({
      sublistId: 'item'
   });
}

itemReceiptRec.save(); 

Also change 'isDynamic' from false to true.

var itemReceiptRec = record.transform({
                fromType: record.Type.PURCHASE_ORDER,
                fromId: csvPOId,
                toType: record.Type.ITEM_RECEIPT,
                isDynamic: true
});

Let me know for any issues in comments below

You are not setting other things like binnumber or issueinventorynumber . Unless the item you are receiving needs to go to a bin or has an inbound serial or lot number then you likely don't need to do anything with the inventoryassignment sublist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM