2
votes

I'm trying to transform an item fulfillment to invoice. I created a custom field called "freight cost" and I"m trying to get the value of that field and transfer it over to the invoice and add two lines to the item sublist: "FREIGHT" and "HANDLING". However, I'm getting an error when I try to get the value of the freight cost.

Here's my code:

/** * @NApiVersion 2.x * @NScriptType UserEventScript * @NModuleScope SameAccount */ define(['N/record', 'N/log'],

function(record, log) {

function afterSubmit(context) {
    var orderId = context.newRecord;
    var freightCost = orderId.record.getValue({
        fieldId:'custbody_freight_cost'
    });
    log.error({
        title: 'Freight Cost',
        details: freightCost
    });
    var invoiceRecord = record.transform({
        fromType: record.Type.ITEM_FULFILLMENT,
        fromId: orderId,
        toType: record.Type.INVOICE,
        isDynamic: true
    });
    log.error({
        title: 'Debug Entry',
        details: invoiceRecord
    });
    var freightLine = invoiceRecord.insertLine({
        sublistId:'item',
        item: 3,
        ignoreRecalc: true
    });
    var handlingLine = invoiceRecord.insertLine({
        sublistId:'item',
        item: 4,
        ignoreRecalc: true
    });
    var freightSaver = invoiceRecord.setCurrentSublistValue({
        sublistId:'item',
        fieldId:'custbody_freight_cost',
        value: freightCost,
        ignoreFieldChange: true
    });
    var rid = invoiceRecord.save();
}

return {
    afterSubmit: afterSubmit
};

});

And here's the error I'm getting:

org.mozilla.javascript.EcmaError: TypeError: Cannot call method "getValue" of undefined (/SuiteScripts/complexInvoice.js#12)

1

1 Answers

2
votes

The reason you're getting that error is because you are calling the .getValue method on the record object instead of the orderId object. I would recommend renaming your variables to avoid some confusion as I have done below.

Another issue I see occurring in this script is that you are not allowed to transform an Item Fulfillment into an Invoice in SuiteScript. You can only transform a Sales Order into an Invoice. If you want to look at all the possible transformations you can make in SuiteScript open up NetSuite help and search for record.transform(options).

Lastly, it looks like you're adding sublist lines to the new invoice in an unusual way. See my code below for an example of how to add lines to an invoice record in "dynamic" mode.

/** 
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
*/
define(["N/record", "N/log"], function (record, log) {

    function afterSubmit(context) {

        // Gather your variables
        var newRec = context.newRecord;
        var freightCost = newRec.getValue({
            fieldId: 'custbody_freight_cost'
        });
        var salesOrderId = newRec.getValue({ // Here we are getting the sales order id to use in the transform function
            fieldId: 'createdfrom'
        });
        log.error({
            title: 'Freight Cost',
            details: freightCost
        });

        // Transform the Sales Order into an Invoice
        var invoiceRecord = record.transform({
            fromType: record.Type.SALES_ORDER,
            fromId: salesOrderId,
            toType: record.Type.INVOICE,
            isDynamic: true
        });
        log.error({
            title: 'Debug Entry',
            details: invoiceRecord
        });

        // Add lines to the invoice like this, this is the correct way when the record is in "dynamic" mode
        invoiceRecord.selectNewLine({
            sublistId: 'item'
        });
        invoiceRecord.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'item',
            value: 3
        });
        invoiceRecord.commitLine({
            sublistId: 'item'
        });
        invoiceRecord.selectNewLine({
            sublistId: 'item'
        });
        invoiceRecord.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'item',
            value: 4
        });
        invoiceRecord.commitLine({
            sublistId: 'item'
        });

        // Here is how you set a body field
        invoiceRecord.setValue({
            fieldId: 'custbody_freight_cost',
            value: freightCost,
            ignoreFieldChange: true
        });

        // Submit the record
        var rid = invoiceRecord.save();
        log.debug('Saved Record', rid);
    }
    return { afterSubmit: afterSubmit };
});