I've been trying to insert a line item (insurance cost) after the user clicks on Save on a transaction. First, I did it with a client script, but the sync issues were too constricting. So I then moved to a User Event script (beforeSubmit). Here is the code:
function beforeSubmit() {
addInsuranceLine();
}
function addInsuranceLine() {
var record = nlapiGetNewRecord();
record.insertLineItem('item', record.getLineItemCount('item'));
record.setLineItemValue('item', 'item', record.getLineItemCount('item'), 21);
record.setLineItemValue('item', 'quantity', record.getLineItemCount('item'), 1);
record.setLineItemValue('item','rate', record.getLineItemCount('item'), 0.01);
record.setLineItemValue('item','amount', record.getLineItemCount('item'), 0.01);
}
Note that 21 is the internal ID of the item I want to add.
I tried hardcoding the line number, setting more or less fields, but the end result is always one error or another. Here are some of the errors I've received:
"Can't find method nlobjRecord.insertLineItem(string). (beforeSubmit#7)" (Dev Guide says the line number is optional, yet I get this error when I try to only enter the group)
"An unexpected SuiteScript error has occurred" (my personal favorite)
"Invalid reference key [null]"
Server-side scripts are fairly new to me, so am I doing something wrong?
Thanks!