2

When creating a new sales order in Suitescript and setting a sublist value for item, an error of INVALID_FLD_VALUE is thrown.

The value I'm passing is the internal id for the item, I have tried with multiple items' internal ids, both with and without quotes and receive the same error. Code is below

/**
 * @NApiVersion 2.0
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */
define(['N/record'], function (r) {
    function get(context) {
        try {
            // Create new record type of SALES_ORDER
            var salesOrder = r.create({
                type: r.Type.SALES_ORDER,
                isDynamic: false,
                defaultValues: null
            })

            // CREATE AN ITEM AND SET VALUES
            salesOrder.insertLine({
                sublistId: 'item',
                line: 0
            });


            // Item Intetrnal ID
            salesOrder.setSublistValue({
                sublistId: 'item',
                fieldId: 'item',
                line: 0,
                value: '15'
            });

            // Quantity
            salesOrder.setSublistValue({
                sublistId: 'item',
                fieldId: 'quantity',
                line: 0,
                value: 4
            });


            salesOrder.save();

            return JSON.stringify('Sales Order Created');
        }
        catch (err) {
            log.audit({
                title:'Error',
                details: err
            })

            return JSON.stringify(err);
        }
    }
    return {
        get: get
    }
})

I have seen tutorials written with this code almost line for line, making me wonder if this has to do with a feature or setting in NetSuite that needs to be turned on/off. Any feedback is greatly appreciated.

quarks
  • 33,478
  • 73
  • 290
  • 513
Bsharp
  • 41
  • 1
  • 6
  • Try losing the `insertLine()` call. My suspicion is that by calling that first, you are inserting a new line and filling it out, leaving the original line blank at index `1`. – erictgrubaugh May 20 '19 at 02:47
  • Thanks for the feedback, that did not do the trick unfortunately. I'm still seeing {"type":"error.SuiteScriptError","name":"INVALID_FLD_VALUE","message":"You have entered an Invalid Field Value 15 for the following field: item" – Bsharp May 20 '19 at 14:07
  • `15` is likely not a valid ID for the transaction and subsidiary you're using. – erictgrubaugh May 20 '19 at 16:23
  • I got that value of 15 by going to the inventory item and hitting view, that is the value under Internal Id. Is there a different Id field that I should be using instead? – Bsharp May 20 '19 at 17:11
  • That's the right approach, but there might be a different reason why you can't add that specific item to the transaction. I'm not sure what it might be, could be anything from Subsidiary mismatch. – erictgrubaugh May 20 '19 at 21:13

1 Answers1

2

The error was caused because I was not setting the "entity" field before proceeding to adding sublist items. So the error was not actually a result of the item id value.

Code that works:

var salesOrder = r.create({
                type: r.Type.SALES_ORDER,
                isDynamic: true,
                defaultValues: Date.now().toString
            }).setValue({
                fieldId: "entity",
                value: customer
            })                                                              

            salesOrder.selectNewLine({ sublistId: "item" });

            salesOrder.setCurrentSublistValue({
                sublistId: "item",
                fieldId: "item",
                value: itemId
            });
            salesOrder.setCurrentSublistValue({
                sublistId: "item",
                fieldId: "quantity",
                value: 5
            });
            salesOrder.commitLine({ sublistId: "item" });

            salesOrder.save({
                enableSourcing: true,
                ignoreMandatoryFields: true
            })
Bsharp
  • 41
  • 1
  • 6
  • I ran into the same error with a custom record type. I tried filling in a few more fields that looked important and the error went away. Thank you for posting this answer, and I hope one day NetSuite will fix this misleading error message! – Marty C. Dec 23 '22 at 04:46