Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Id field throwing INVALID_FLD_VALUE in SuiteScript

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.

like image 309
Bsharp Avatar asked Sep 16 '25 02:09

Bsharp


1 Answers

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
            })
like image 80
Bsharp Avatar answered Sep 17 '25 17:09

Bsharp