Netsuite Suitescript 2.0 Inventory Transfer on Lot items

46 Views Asked by At

i am having diffculty gettig my fucntion in suitescript 2.0 to work to transfer lot items on netsuite. I keep getting an error "Error: You cannot create an inventory detail for this item.". Even though i am certain the internal ids i am using are lot items . See function below

function transferItems(itemIds, TRANSFER_LOCATION_ID) {
      log.debug('Start transferItems', 'Starting item transfer process for items: ' + JSON.stringify(itemIds) + ' to location: ' + TRANSFER_LOCATION_ID);
      itemIds.forEach(function(item) {
        var itemId = 519;
        var itemLength = item.length; 
        var lotNumber = item.netsuiteId; 
        log.debug('Processing item', 'Item ID: ' + itemId + ', Length: ' + itemLength + ', Lot: ' + lotNumber);

        try {
            var transferRecord = record.create({
                type: record.Type.INVENTORY_TRANSFER,
                isDynamic: true
            });
            log.debug('Transfer record created', 'Transfer record initiated for item ID: ' + itemId);

            // Set dynamic values for fields here
            transferRecord.setValue({fieldId: 'subsidiary', value: '7'}); // Example subsidiary ID
            transferRecord.setValue({fieldId: 'trandate', value: new Date()});
            transferRecord.setValue({fieldId: 'location', value: 903}); // Example source location ID
            transferRecord.setValue({fieldId: 'transferlocation', value: TRANSFER_LOCATION_ID});

            // Add the item to the inventory sublist
            transferRecord.selectNewLine({sublistId: 'inventory'});
            transferRecord.setCurrentSublistValue({sublistId: 'inventory', fieldId: 'item', value: itemId});
            
            // Work with the inventorydetail subrecord for lot numbered items
            var inventoryDetailSubrecord = transferRecord.getCurrentSublistSubrecord({
                sublistId: 'inventory',
                fieldId: 'inventorydetail'
            });
            log.debug(inventoryDetailSubrecord);


            inventoryDetailSubrecord.selectNewLine({sublistId: 'inventoryassignment'});
            inventoryDetailSubrecord.setCurrentSublistValue({sublistId: 'inventoryassignment', fieldId: 'issueinventorynumber', value: lotNumber}); // Set the lot number
            inventoryDetailSubrecord.setCurrentSublistValue({sublistId: 'inventoryassignment', fieldId: 'quantity', value: parseInt(itemLength)});
            
            // Commit the lines
            inventoryDetailSubrecord.commitLine({sublistId: 'inventoryassignment'});
            log.debug('Inventory detail set and committed for lot', 'Lot: ' + lotNumber + ', Quantity: ' + itemLength);

            transferRecord.commitLine({sublistId: 'inventory'});
            
            var transferId = transferRecord.save({enableSourcing: true, ignoreMandatoryFields: true});
            log.debug('Inventory Transfer Created', 'Transfer ID: ' + transferId + ' for Item ID: ' + itemId + ', Lot: ' + lotNumber);
        } catch (e) {
            log.error('Error transferring lot-numbered item', 'Item ID: ' + itemId + ', Lot: ' + lotNumber + ', Error: ' + e.message);
        }
    });
    log.debug('End transferItems', 'Lot-numbered item transfer process completed.');
}

I have tried using the lot item text value and the internal id and i have re ordered the script but its still not working . I am relatively new to suite script

1

There are 1 best solutions below

0
Brian Duffy On

Use hasCurrentSublistSubrecord(options) first to check if the sub record exists otherwise using getCurrentSublistSubrecord will try to create one if it doesn't exist.