2

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)

CodeMonkey
  • 97
  • 2
  • 9

1 Answers1

2

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 };
});
Jon Lamb
  • 1,413
  • 2
  • 16
  • 28
  • Thank you. However, I'm still getting an error: me":"SSS_INVALID_SUBLIST_OPERATION","message":"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.","stack": – CodeMonkey Jul 19 '18 at 20:37
  • Interesting. Can you provide the stack trace for the error? I did test this script in my account and it seemed to be working as expected. Are you sure there are no other scripts running on the invoice that might be affecting this? – Jon Lamb Jul 19 '18 at 21:42
  • Turns out I had another script running, thanks. Now I'm getting this error: {"type":"error.SuiteScriptError","name":"SSS_MISSING_REQD_ARGUMENT","message":"transform: Missing a required argument: fromId","stack":["createError(N/error)","afterSubmit(/SuiteScripts/complexInvoice.js:24)","createError(N/error)"],"cause":{"name":"SSS_MISSING_REQD_ARGUMENT","message":"transform: Missing a required argument: fromId"},"id":"","notifyOff":false} – CodeMonkey Jul 19 '18 at 23:47
  • Can you try logging the Sales Order ID like so `log.debug('Sales Order ID, salesOrderId)` right after this block: `var salesOrderId = newRec.getValue({ // Here we are getting the sales order id to use in the transform function fieldId: 'createdfrom' });`? This variable has to have something in it. If you are running this on an Item Fulfillment record created from a Sales Order this should be filled in every time. – Jon Lamb Jul 20 '18 at 00:49
  • Yeah there's nothing there. The program isn't reading in any value for sales order id – CodeMonkey Jul 20 '18 at 01:03
  • When you look at the Item Fulfillment record in the UI do you see something in the Created From field like in this image? https://imgur.com/a/KGMzAR7 – Jon Lamb Jul 20 '18 at 02:31
  • That's very strange. Is your script running on After Submit for sure? And you're using the `context.NewRecord` object? – Jon Lamb Jul 20 '18 at 20:54
  • Yes, I'm thinking maybe a setting is off or something cause I don't see any reason why this code wouldn't work, this is the code: https://docs.google.com/document/d/1bw5dC6XcCfU6Nc0_nkG8cjCzslSai7eO2pKh59R_i8E/edit?usp=sharing – CodeMonkey Jul 20 '18 at 21:55
  • Yes, that looks good. How are your fulfillments created? Do you click the "Fulfill" button on the Sales Order and the "Save" button on the Fulfillment record to save it, or do you have an automated process? – Jon Lamb Jul 20 '18 at 22:11
  • I click fulfill on the sales order and save it – CodeMonkey Jul 20 '18 at 23:20
  • When you first create the Item Fulfillment, before you hit save, do you see the Sales Order in the created from field? If you open the console in the browser and type `nlapiGetFieldValue('createdfrom');` do you get a value? If you do get something, there might be an issue with the deployment record, could you post a screenshot of your deployment record? – Jon Lamb Jul 21 '18 at 15:36
  • I get null when I type nlapiGetFieldValue('createdfrom'); in the console. Here is the deployment record: https://photos.app.goo.gl/Dpp2xKjkwZdmu7Eu9 – CodeMonkey Jul 21 '18 at 18:42
  • I deployed it to item fulfillment and I now get a value for the sales order id, however I'm getting a new error: "INVALID_FLD_VALUE","message":"You have entered an Invalid Field Value 3 for the following field: item","stack" – CodeMonkey Jul 21 '18 at 23:05
  • I figured it out. Thank you so much for your help. It's nice that you devote so much time to people here. – CodeMonkey Jul 21 '18 at 23:25