netsuitesuitescript2.0abrecordref

How can i get a recordRef in SuiteScript 2.0?


i'm trying to write a Restlet to create a sales order in NetSuite and need put an item recordRef type in a field, but i don't know how can i get it, or how can i write it. Please, can you help me?

Here is the recordRef schema https://www.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2017_1/schema/other/recordref.html?mode=package

Next my script =>

function CreateNetSuiteOrder(requestBody) {

        var salesOrder = record.create({
            type: record.Type.SALES_ORDER,
            isDynamic: true,
            defaultValues: {
                entity: 9999999999
            }
        });

        salesOrder.setValue({ fieldId: 'trandate', value: new Date('5/07/2020') });

        var subrec = salesOrder.getSubrecord({
            fieldId: 'shippingaddress'
        });

        subrec.setValue({ fieldId: 'addr1', value: '123 street' });
        subrec.setValue({ fieldId: 'city', value: 'city' });
        subrec.setValue({ fieldId: 'state', value: 'state' });
        subrec.setValue({ fieldId: 'zip', value: 'CA' });
        subrec.setValue({ fieldId: 'addressee', value: 'John' });
        subrec.setValue({ fieldId: 'attention', value: 'John' });


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

        salesOrder.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'item',
                value: // i should put RecordRef here
        });

            salesOrder.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'quantity',
                // value: item.quantity_order
                value: 3
            });

            salesOrder.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'rate',
                value: 34.0
            });

            salesOrder.commitLine({
                sublistId: 'item'

            });

            salesOrder.save(
                {
                 enableSourcing: false,
                ignoreMandatoryFields: true

} )


Solution

  • A RecordRef is another class that exported in the NetSuite wsdl. It's used mostly in select-type fields where you can specify either the internal id or the text of the value you want to put in that field.

    salesOrder.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'item',
        value: new RecordRef() {
           internalId: "123"
        }
    });
    

    I use some extension methods to make working with RecordRefs a bit easier. They look something like this:

    public static class NsExtensions
    {
        public static RecordRef ToRecordRef(this int value)
        {
            return new RecordRef
            {
                internalId = value.ToString()
            };
        }
    }
    

    With this extension in place, the code above would look like this:

    salesOrder.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'item',
        value: 123.ToRecordRef()
    });