RE: in SS2.x, trying to add an item to item sublist in SUE [AfterSubmit] but results in error

Hi,

I am trying to create a SuiteScript 2.0 that will automatically add a <Payment> type item to a Cash Sales if an open Credit Memo exists for a client.

I am hitting a wall simply adding the item to the item sublist. I am in SUE [AfterSubmit] and since I have no loaded the cash sale as I am creating it from the GUI (Enter Cash Sale), the cashsaleRec is a standard object (not dynamic).

 

var cashsaleRec = scriptContext.newRecord;
var numLines = cashsaleRec.getLineCount({
sublistId: 'item'
});
logModule.debug({
title: 'lines',
details: numLines+1
});
cashsaleRec.insertLine({
sublistId: 'item',
line: numLines+1,
});
cashsaleRec.setSublistValue({
sublistId: 'item',
fieldId: 'item',
line: numLines+1,
value: 2756
});
cashsaleRec.setSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: numLines+1,
value: -1
});
cashsaleRec.commitLine({
sublistId: 'item'
});

Error Message:

{“type”:”error.SuiteScriptError”,”name”:”UNEXPECTED_ERROR”,”message”:null,”stack”:[“anonymous(N/serverRecordService)”,”afterSubmit(/SuiteScripts/AXE_SUE_CreditApplyonCS.js:124)”],”cause”:{“type”:”internal error”,”code”:”UNEXPECTED_ERROR”,”details”:null,”userEvent”:”aftersubmit”,”stackTrace”:[“anonymous(N/serverRecordService)”,”afterSubmit(/SuiteScripts/AXE_SUE_CreditApplyonCS.js:124)”],”notifyOff”:false},”id”:”f8800b9a-a2f9-4748-8ed0-9df662eeb730-2d323031392e30392e3136″,”notifyOff”:false,”userFacing”:false}

The record is still saved but without the additional line I am trying to add ?

Can anyone help me please?

 

FYI to minimize system interferance I am checking the ‘credit card approved’ box in <Billing> to bypass the Payment Gateway for my tests.

Also I am testing this out in the Release Preview login.

mcfly Rookie Asked on September 16, 2019 in SuiteScript.
Add Comment
1 Answers
Best answer

There are several things that can cause an issue here

SuiteScript 2.0 is 0 index based. If there are 3 existing lines on a sales order, then the new line is index 3. Don’t add 1 to the line count. (you probably also don’t need the insertLine or commitLine)

The  newRecord from your scriptContext cannot be saved. You have to load the record again and save it. Its also the reason why modifying the current record in afterSubmit is terrible for performance.

Advanced Answered on September 16, 2019.

I forgot about SuiteScript 2.0 being 0 index based. It’s totally what was throwing the unexpected error. Thank you! As for performance, I do see the issue. 

I moved my code to the BeforeSubmit function and it’s performing much better !

on September 16, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.