使用SuiteScript创建供应商账单时如何设置库存明细?

时间:2019-01-11 18:17:48

标签: netsuite suitescript2.0

我正在尝试用SuiteScript创建供应商账单,该账单将由入库货件生成。它还将检索项目收货的库存编号以设置库存明细。但是,我无法弄清楚如何在SuiteScript中进行设置,因为在调试时没有看到允许我为此设置值的字段(当前测试并且希望在进行下一步操作之前进行显式设置)。我的代码示例如下:

define(['N/record','N/redirect','N/search'],
function(record, redirect, search) {

/**
 * Definition of the Suitelet script trigger point.
 *
 * @param {Object} context
 * @param {ServerRequest} context.request - Encapsulation of the incoming request
 * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
 * @Since 2015.2
 */
function onRequest(context) {

    var request = context.request;
    var response = context.response;

    var id = context.request.parameters.custom_id;
    if (!id) {
        context.response.write('The parameter "custom_id" is required');
        return;
    }
    else {
        alert('ID is ' + id);
    }

    var loadedRecord = record.load({
        type: record.Type.INBOUND_SHIPMENT,
        id: id
    });

    alert('Generating vendor bill');

    var shipmentBaseCurrency = loadedRecord.getValue('shipmentbasecurrency');

    var vendorBill = record.create({
        type: record.Type.VENDOR_BILL,
        isDynamic: true
    });

    // retrieve item receipt for inventory details
    var itemReceiptSearchResults = search.create({
        type: "itemreceipt",
        filters:
        [
           ["type","anyof","ItemRcpt"]
           // needs to be linked to shipment
        ],
        columns:
        [
           search.createColumn({name: "mainline", label: "*"}),
           search.createColumn({
              name: "trandate",
              sort: search.Sort.ASC,
              label: "Date"
           }),
           search.createColumn({name: "asofdate", label: "As-Of Date"}),
           search.createColumn({name: "postingperiod", label: "Period"}),
           search.createColumn({name: "taxperiod", label: "Tax Period"}),
           search.createColumn({name: "type", label: "Type"}),
           search.createColumn({name: "tranid", label: "Document Number"}),
           search.createColumn({name: "entity", label: "Name"}),
           search.createColumn({name: "account", label: "Account"}),
           search.createColumn({name: "memo", label: "Memo"}),
           search.createColumn({name: "amount", label: "Amount"}),

           search.createColumn({
              name: "inventorynumber",
              join: "inventoryDetail"
        })
        ]
     });

    var itemRecIdObj = itemReceiptSearchResults.run().getRange({start:0, end:1});

    var itemRecLines = [];

    if (itemRecIdObj.length > 0) {
        // loop through items
        var lines = loadedRecord.getLineCount({sublistId: 'items'});
        log.debug("item receipt lines: " + lines);

        for (var i = 0; i < lines; i++) {
            var item = loadedRecord.getSublistValue({
                sublistId: "items",
                fieldId: "item",
                line: i
            });

            var inventoryDetail = loadedRecord.getSublistValue({
                sublistId: "items",
                fieldId: "inventorynumber",
                line: i
            });

            var itemRecValues = {
                inventoryDetail: inventoryDetail,
                item: item
            };

            itemRecLines.push(itemRecValues);
        }
    }

    vendorBill.setValue('currency', shipmentBaseCurrency);

    var receivingLocation = '';

    var lines = loadedRecord.getLineCount({sublistId: 'items'});
    log.debug("tran. lines: " + lines);

    var total;

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

        var quantity = (parseFloat(loadedRecord.getSublistValue({
            sublistId: "items",
            fieldId: "quantityreceived",
            line: i
        })) || 0);

        var purchaseOrder = loadedRecord.getSublistValue({
            sublistId: "items",
            fieldId: "purchaseorder",
            line: i
        });

        var item = loadedRecord.getSublistValue({
            sublistId: "items",
            fieldId: "itemid",
            line: i
        });

        var description = loadedRecord.getSublistValue({
            sublistId: "items",
            fieldId: "shipmentitemdescription",
            line: i
        });

        var recLoc = loadedRecord.getSublistValue({
            sublistId: "items",
            fieldId: "receivinglocation",
            line: i
        });

        receivingLocation = recLoc;

        var totalUnitCost = loadedRecord.getSublistValue({
            sublistId: "items",
            fieldId: "totalunitcost",
            line: i
        });

        var amount = loadedRecord.getSublistValue({
            sublistId: "items",
            fieldId: "shipmentitemamount",
            line: i
        });

        log.debug("item quantity: " + quantity);

        vendorBill = addVendorBillLine(vendorBill, item, quantity, receivingLocation, itemRecLines);

    } // for (var i = 0; i < lines; i++)

    var vbPostPeriodValStr = String(getPostingPeriod());
    try {
        vendorBill.setText('postingperiod', vbPostPeriodValStr);
    }
    catch(e) {
        log.error('Issue setting posting period for vendor bill', e.message);
    }

    vendorBill.setValue('location', receivingLocation);

    try {
        var vendorBillId = vendorBill.save(); 
        log.debug('*******Vendor Bill Saved*******', 'Vendor bill successfully saved: ' + vendorBillId);
        redirect.toRecord({
            type: record.Type.VENDOR_BILL, 
            id: vendorBillId 
        });
    }
    catch(e) {
        log.debug('Error saving vendor bill', e.message);
        context.response.write('Error: ' + e.message);
    }
}

function addVendorBillLine(vendorBill, item, quantity, location, itemRecLines) {
    try {
        var lineNum = vendorBill.selectNewLine({
            sublistId: 'item'
        });

        vendorBill.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "item",
            value: item
        });

        vendorBill.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "quantity",
            value: quantity
        });

        vendorBill.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "location",
            value: location
        });

        vendorBill.setCurrentSublistValue({
            sublistId: "item",
            fieldId: "itemtype",
            value: 'NonInvtPart'
        });

        // vendorBill.setCurrentSublistValue({
        //     sublistId: "item",
        //     fieldId: "inventorydetailavail",
        //     value: 'F'
        // });

        // vendorBill.setCurrentSublistValue({
        //     sublistId: "item",
        //     fieldId: "inventorydetailreq",
        //     value: 'F'
        // });

        var itemRecObjInd = itemRecLines.map(function (x) { return x.item; }).indexOf(item);
        var itemRecObj = itemRecLines[itemRecObjInd];
        var inventoryDetail = itemRecObj.inventoryDetail;
        var irItem = itemRecObj.item;

        var inventoryDetailRecord = vendorBill.getSublistSubrecord({
            sublistId: 'item',
            fieldId: 'inventorydetail',
            line: lineNum
        });

        var hasSubrecord = vendorBill.hasCurrentSublistSubrecord({
            sublistId: 'item',
            fieldId: 'inventorydetail'
        });

        if(hasSubrecord){
        var inventoryDetailRecord = vendorBill.getCurrentSublistSubrecord({
            sublistId: 'item',
            fieldId: 'inventorydetail'
        });

        inventoryDetailRecord.setSublistValue({
            sublistId: 'inventoryassignment',
            fieldId: 'issueinventorynumber',
            value: '4344111'
        });
        }

        vendorBill.commitLine({sublistId:"item"});

        log.debug('Committing vendor bill expense line addition','Name: ' + item + ", Quantity: " + quantity);
    }
    catch(e){
        log.debug('Issue committing vendor bill item line addition', e.message);
    }

    return vendorBill;
}

function getPostingPeriod() {
    var monthAbbr = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    ];

    var d = new Date();
    return monthAbbr[d.getMonth()] + ' ' + d.getFullYear();
}

return {
    onRequest: onRequest
};

});

1 个答案:

答案 0 :(得分:0)

您的代码似乎未在库存详细信息子记录中设置数量。库存明细子记录中的数量是必需的和必需的

您可以使用下面的代码示例来设置库存明细中的数量和其他字段。

var inventoryDetailRecord = vendorBill.getSublistSubrecord({
  sublistId: 'item',
  fieldId: 'inventorydetail',
  line: lineNum
});

// set lot/serial-number
inventoryDetailRecord.setSublistValue({
  sublistId: 'inventoryassignment',
  fieldId: 'issueinventorynumber',
  value: '4344111'
});

// set bin number
inventoryDetailRecord.setSublistValue({
  sublistId: 'inventoryassignment',
  fieldId: 'binnumber',
  value: BIN_NUMBER
});

// set quantity

inventoryDetailRecord.setSublistValue({
  sublistId: 'inventoryassignment',
  fieldId: 'quantity',
  value: quantity
});

Here是NetSuite记录浏览器文档,其中包含库存明细记录中的字段列表。