admin管理员组

文章数量:1278854

I would like to implement a feature to repopulate unfulfilled items from a purchase order onto an existing item receipt. situation is a little confusion so let me use a scenario: lets say a purchase order contains 3 items on separate lines. A user receives only one of these items and creates an item receipt, leaving the other two lines unchecked. Currently upon saving, only the received item appears on the receipt, and NetSuite does not allow adding the remaining items back to that receipt for further receiving. The only allowed action on edit is to either adjust the quantity of the already received item or create a new item receipt to recieve the other two. I want to write a suitlet add a feature that will allow the item receipt to repopulate on edit so it can be received whole on the same receipt

I've created a button on the action bar with DOM manipulation to trigger the following script in edit view

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/record', 'N/log'], function (record, log) {
    function onRequest(context) {
        log.debug('Suitelet Entry', 'The onRequest function has been triggered.');

        try {
            // Gets request parameters
            var params = context.request.parameters;
            log.debug('Request Parameters', params);

            var itemReceiptId = params.recordId;

            if (!itemReceiptId) {
                log.error('Error', 'No Item Receipt ID provided.');
                return;
            }

            log.debug('Item Receipt ID Received', itemReceiptId);

            // Load the Item Receipt record as dynamic
            var itemReceipt = record.load({
                type: record.Type.ITEM_RECEIPT,
                id: itemReceiptId,
                isDynamic: true // dynamic mode for line manipulation? dont know if this is right
            });

            log.debug('Item Receipt Loaded', 'Item Receipt ID: ' + itemReceiptId);

            // Get linked Purchase Order ID from Item Receipt
            var poId = itemReceipt.getValue('createdfrom');
            if (!poId) {
                log.error('Error', 'The Item Receipt is not linked to any Purchase Order.');
                return;
            }

            log.debug('Linked PO ID Retrieved', poId);

            // Load the Purchase Order record
            var poRecord = record.load({
                type: record.Type.PURCHASE_ORDER,
                id: poId
            });

            var poLineCount = poRecord.getLineCount({ sublistId: 'item' });
            var irLineCount = itemReceipt.getLineCount({ sublistId: 'item' });

            log.debug('Line Counts Retrieved', 'PO Lines: ' + poLineCount + ', IR Lines: ' + irLineCount);

            // Track existing items in Item Receipt using  array
            var existingItems = [];
            for (var i = 0; i < irLineCount; i++) {
                itemReceipt.selectLine({ sublistId: 'item', line: i });
                var irItem = itemReceipt.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item'
                });
                existingItems.push(irItem);
                log.debug('Existing Item in IR', 'Line ' + (i + 1) + ': ' + irItem);
            }

            log.debug('Existing Items Array', JSON.stringify(existingItems));

            // Compare PO lines and the IR lines to add missing lines
            for (var i = 0; i < poLineCount; i++) {
                var poItem = poRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'item',
                    line: i
                });

                // Check if the PO item exists in the Item Receipt using a manual loop
                var itemExists = false;
                for (var j = 0; j < existingItems.length; j++) {
                    if (existingItems[j] == poItem) { // Useing strict equality to compare
                        itemExists = true;
                        break;
                    }
                }

                if (!itemExists) {
                    log.debug('Missing Line Found', 'Adding PO Item ID: ' + poItem);

                    // Add new line to the dynamic sublist
                    try {
                        itemReceipt.selectNewLine({ sublistId: 'item' });
                        itemReceipt.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'item',
                            value: poItem
                        });
                        itemReceipt.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'quantity',
                            value: poRecord.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'quantity',
                                line: i
                            })
                        });
                        itemReceiptmitLine({ sublistId: 'item' });

                        log.debug('Missing Line Added', 'PO Item ID ' + poItem + ' added to Item Receipt.');
                    } catch (e) {
                        log.error('Error Adding Line', 'Failed to add PO Item ID ' + poItem + ': ' + e.message);
                    }
                }
            }

            // Save updated Item Receipt record
            try {
                var updatedRecordId = itemReceipt.save();
                log.debug('Resync Completed', 'Item Receipt ' + updatedRecordId + ' updated successfully.');
            } catch (e) {
                log.error('Error Saving Item Receipt', e.message);
            }

            // Redirect back 2 Item Receipt record in Edit mode
            context.response.sendRedirect({
                type: context.response.RedirectType.RECORD,
                identifier: record.Type.ITEM_RECEIPT,
                id: itemReceiptId,
                isEditMode: true
            });
        } catch (e) {
            log.error('Error During Resync Process', e.message);
        }
    }

    return {
        onRequest: onRequest
    };
});

i keep getting error when adding the line: "You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist."

I'm new to NetSuite Suite script so whatever corrections and opinions are welcomed, maybe I'm missing the logic here.

I would like to implement a feature to repopulate unfulfilled items from a purchase order onto an existing item receipt. situation is a little confusion so let me use a scenario: lets say a purchase order contains 3 items on separate lines. A user receives only one of these items and creates an item receipt, leaving the other two lines unchecked. Currently upon saving, only the received item appears on the receipt, and NetSuite does not allow adding the remaining items back to that receipt for further receiving. The only allowed action on edit is to either adjust the quantity of the already received item or create a new item receipt to recieve the other two. I want to write a suitlet add a feature that will allow the item receipt to repopulate on edit so it can be received whole on the same receipt

I've created a button on the action bar with DOM manipulation to trigger the following script in edit view

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/record', 'N/log'], function (record, log) {
    function onRequest(context) {
        log.debug('Suitelet Entry', 'The onRequest function has been triggered.');

        try {
            // Gets request parameters
            var params = context.request.parameters;
            log.debug('Request Parameters', params);

            var itemReceiptId = params.recordId;

            if (!itemReceiptId) {
                log.error('Error', 'No Item Receipt ID provided.');
                return;
            }

            log.debug('Item Receipt ID Received', itemReceiptId);

            // Load the Item Receipt record as dynamic
            var itemReceipt = record.load({
                type: record.Type.ITEM_RECEIPT,
                id: itemReceiptId,
                isDynamic: true // dynamic mode for line manipulation? dont know if this is right
            });

            log.debug('Item Receipt Loaded', 'Item Receipt ID: ' + itemReceiptId);

            // Get linked Purchase Order ID from Item Receipt
            var poId = itemReceipt.getValue('createdfrom');
            if (!poId) {
                log.error('Error', 'The Item Receipt is not linked to any Purchase Order.');
                return;
            }

            log.debug('Linked PO ID Retrieved', poId);

            // Load the Purchase Order record
            var poRecord = record.load({
                type: record.Type.PURCHASE_ORDER,
                id: poId
            });

            var poLineCount = poRecord.getLineCount({ sublistId: 'item' });
            var irLineCount = itemReceipt.getLineCount({ sublistId: 'item' });

            log.debug('Line Counts Retrieved', 'PO Lines: ' + poLineCount + ', IR Lines: ' + irLineCount);

            // Track existing items in Item Receipt using  array
            var existingItems = [];
            for (var i = 0; i < irLineCount; i++) {
                itemReceipt.selectLine({ sublistId: 'item', line: i });
                var irItem = itemReceipt.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item'
                });
                existingItems.push(irItem);
                log.debug('Existing Item in IR', 'Line ' + (i + 1) + ': ' + irItem);
            }

            log.debug('Existing Items Array', JSON.stringify(existingItems));

            // Compare PO lines and the IR lines to add missing lines
            for (var i = 0; i < poLineCount; i++) {
                var poItem = poRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'item',
                    line: i
                });

                // Check if the PO item exists in the Item Receipt using a manual loop
                var itemExists = false;
                for (var j = 0; j < existingItems.length; j++) {
                    if (existingItems[j] == poItem) { // Useing strict equality to compare
                        itemExists = true;
                        break;
                    }
                }

                if (!itemExists) {
                    log.debug('Missing Line Found', 'Adding PO Item ID: ' + poItem);

                    // Add new line to the dynamic sublist
                    try {
                        itemReceipt.selectNewLine({ sublistId: 'item' });
                        itemReceipt.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'item',
                            value: poItem
                        });
                        itemReceipt.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'quantity',
                            value: poRecord.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'quantity',
                                line: i
                            })
                        });
                        itemReceiptmitLine({ sublistId: 'item' });

                        log.debug('Missing Line Added', 'PO Item ID ' + poItem + ' added to Item Receipt.');
                    } catch (e) {
                        log.error('Error Adding Line', 'Failed to add PO Item ID ' + poItem + ': ' + e.message);
                    }
                }
            }

            // Save updated Item Receipt record
            try {
                var updatedRecordId = itemReceipt.save();
                log.debug('Resync Completed', 'Item Receipt ' + updatedRecordId + ' updated successfully.');
            } catch (e) {
                log.error('Error Saving Item Receipt', e.message);
            }

            // Redirect back 2 Item Receipt record in Edit mode
            context.response.sendRedirect({
                type: context.response.RedirectType.RECORD,
                identifier: record.Type.ITEM_RECEIPT,
                id: itemReceiptId,
                isEditMode: true
            });
        } catch (e) {
            log.error('Error During Resync Process', e.message);
        }
    }

    return {
        onRequest: onRequest
    };
});

i keep getting error when adding the line: "You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist."

I'm new to NetSuite Suite script so whatever corrections and opinions are welcomed, maybe I'm missing the logic here.

Share Improve this question asked Feb 25 at 5:36 GhostsuGhostsu 1
Add a comment  | 

1 Answer 1

Reset to default 0

The "NetSuite way" to accomplish this without customization is to have multiple Item Receipts per Purchase Order, only creating an Item Receipt when you've actually received some items.

I would not advise trying to force a 1:1 relationship between PO and IR. Creating GL-impacting transactions ahead of time and editing them later can be a massive accounting headache - particularly when the edits span across accounting periods.

本文标签: suitescriptNetSuite Repopulate unfulfilled item on recorded Item Receipt FormStack Overflow