i want to set the sublist values on the invoice re...
# suitescript
v
i want to set the sublist values on the invoice recotd. i used aftersubmit no success, then used before submit, still no success. could anyone please help? if(lineID === (i+1)/2){ newRecord.selectLine({ sublistId : 'item', line : i+1 }) newRecord.setCurrentSublistValue({ sublistId : 'item', fieldId : 'quantity', //line : i + 1, value : quantity }); newRecord.commitLine({ sublistId : 'item' }); } newRecord.save({ enableSourcing: true, ignoreMandatoryFields: true });
s
if you are making changes in before submit, you are editing a live record, and it will save itself (don’t call record.save). if it’s an after submit, the record is no longer live, so you must load the record, edit it, then save. there shouldn’t be any need for an after submit just to change sublist values, though. the
lineID === (i+1)/2
part confuses me, though. what is that logic trying to accomplish? where do the values of
lineID
and
i
get set/updated?
Also, be aware of the difference between standard and dynamic mode. The sublist methods you are using only work for dynamic mode. Please take the time to read and understand this: https://suiteanswers.custhelp.com/app/answers/detail/a_id/73792#bridgehead_1524158046
v
lineID === (i+1)/2 its just some logic if condition.
var invoiceRecord = record.load({ type : 'invoice', id : newRecordID, isDynamic : true }); var lineCount = invoiceRecord.getLineCount('item'); log.debug('line',lineCount); for(var i = 1; i <= lineCount; i+2){ log.debug('i',i); var item = invoiceRecord.getSublistValue({ sublistId : 'item', fieldId : 'item', line : i }); log.debug('item',item); var quantity = invoiceRecord.getSublistValue({ sublistId : 'item', fieldId : 'quantity', line : i }); log.debug('quantity',quantity); invoiceRecord.selectLine({ sublistId : 'item', line : i+1 }) invoiceRecord.setCurrentSublistValue({ sublistId : 'item', fieldId : 'quantity', //line : i + 1, value : quantity }); invoiceRecord.commitLine({ sublistId : 'item' }); } } } invoiceRecord.save();
still i dont get the success
s
you haven’t explained what your success criteria are
but also, you definitely did not read and understand the SuiteAnswers article I provided, as some of the code will not work in a user event, and that article will explain why
v
i just setting the sublist values of invoice in afterSubmit
s
actually, i do see that you are loading the record in dynamic mode, so you are right, that part is fine
it then comes down to your logic in the for loop
for example, why do you start at the second line of the list? and, is the only purpose to overwrite the quantity on the third, fifth, seventh lines (and so on) with the quantity from the previous line (second, fourth, sixth)?
v
but it didnt set the value
s
how many line items does the invoice record have?
v
only 4
s
the third line’s quantity should be set to match the second line’s after running. is that not happening?
v
no (1 & 2), (3 & 4)
1st quantity should be set on 2nd line 3rd quantity should be set on 4th line
s
line #’s start at 0 in SuiteScript 2.x
you are starting at 1, skipping the first line
v
no that does not work even
var invoiceRecord = record.load({ type : 'invoice', id : newRecordID, isDynamic : true }); var lineCount = invoiceRecord.getLineCount('item'); log.debug('line',lineCount); for(var i = 0; i < lineCount; i + 2){ log.debug('i',i); var quantity = invoiceRecord.getSublistValue({ sublistId : 'item', fieldId : 'quantity', line : i }); log.debug('item',item); log.debug('item if',item); invoiceRecord.selectLine({ sublistId : 'item', line : (i + 2) /2 }); invoiceRecord.setCurrentSublistValue({ sublistId : 'item', fieldId : 'quantity', value : quantity }); invoiceRecord.commitLine({ sublistId : 'item' }); } } } invoiceRecord.save();
s
check the arithmetic here. (0 + 2) /2 = 0 (2 + 2) /2 = 2 which means you are taking line 0
and setting the quantity back on line 0
then for line 2, again you are setting the value back to line 2
if you have 5 or more lines, then the next one would be: (4 + 2) /2 = 3 so it would set the fourth line’s quantity to match the fifth