I created a restlet script that is meant to post data into Netsuite to create an invoice record, and return the recordID. The data is being sent from Postman in JSON format.
When I tried it with Suitescript 1.0, it was able to create the record, however when I tried the same logic with Suitescript 2.0, it doesn't work.
The Suitescript 1.0 script did the following upon first request:
- returned 200 OK and the record ID of the newly created record
The Suitescript 2.0 script did the following:
- initially returns error 400: bad request
- upon sending once more, returns status 200OK but no record ID is returned
- when I check the Netsuite UI, no invoice record is created
This is the suitescript 1.0 code (which I found on the internet):
function createRecord(datain)
{
var err = new Object();
var record = nlapiCreateRecord('invoice');
for (var fieldname in datain)
{
if (datain.hasOwnProperty(fieldname))
{
if (fieldname != 'recordtype' && fieldname != 'id')
{
var value = datain[fieldname];
if (value && typeof value != 'object') // process non-item fields
{
record.setFieldValue(fieldname, value);
}
if (value && typeof value == 'object') // process line item objects
{
for (var itemobject in value)
{
record.selectNewLineItem('item');
var lineitemobject= value[itemobject];
for (var lineitemfieldname in lineitemobject )
{
var lineitemfieldvalue = lineitemobject[lineitemfieldname];
record.setCurrentLineItemValue('item',lineitemfieldname,lineitemfieldvalue);
}
record.commitLineItem('item');
}
}
}
}
}
var recordId = nlapiSubmitRecord(record);
return recordId;
}
This is my suitescript 2.0 code:
// Create a NetSuite invoice
/**
* @NApiVersion 2.0
* @NScriptType Restlet
* @NModuleScope SameAccount
*/
//define and declare modules
define(['N/record'], function(record)
{
//function to create a new invoice record with data from API
function createInvoice(datain)
{
//create record
var record = record.create({
type: record.Type.INVOICE,
isDynamic: false,
defaultValues: null
});
for (var fieldname in datain)
{
if (datain.hasOwnProperty(fieldname))
{
if (fieldname!='recordtype' && fieldname!='id')
{
var value = datain[fieldname];
// process non-item fields
if (value && typeof value != 'object')
{
record.setValue({
fieldId: fieldname,
value: value
});
} //close non-item field processing if stmt
// process line item sublist objects
if (value && typeof value == 'object')
{
for (var itemobject in value)
{
record.selectNewLine({sublistId:'item'}); //selects a new line at the end of a sublist
var lineitemobject= value[itemobject];
for (var lineitemfieldname in lineitemobject)
{
var lineitemfieldvalue = lineitemobject[lineitemfieldname];
record.setCurrentSublistValue({
sublistId: 'item',
fieldId: lineitemfieldname,
value: lineitemfieldvalue
});
} //close lineitemobject for loop
record.commitLine({sublistId: 'item'});//Commits the currently selected line on a sublist --> dynamic mode only
} //close value for loop
} //close line item processing if stmt
} //close hasOwnProperty if stmt
} //close datain processing for loop
var recordId = record.save({
enableSourcing: true, //enables sourcing during record update
ignoreMandatoryFields: false
});
return recordId;
} //close create invoice function
return {
'post' : createInvoice
};
}); //close define module declaration
I am super new to Netsuite and especially Suitescript 2.0. Could anyone help me figure out what could have gone wrong while converting 1.0 to 2.0?
don't name your variable record
change 'var record = record.create...' to 'var recordObj = record.create...'
avoid using variable names that are the same as SS 2.x modules