Can anyone help me figure out why the setsublistva...
# suitescript
l
Can anyone help me figure out why the setsublistvalue isn’t working here? Thanks. Code in the replies.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/log'],
function(record, log) {
function beforeLoad(context) {
try {
if (context.type === context.UserEventType.CREATE) {
var newVendorBill = context.newRecord;
var headerFinLoc = newVendorBill.getValue({
fieldId: 'cseg1'
});
log.debug('headerFinLoc: ', headerFinLoc);
var vendorBillLinesCount = newVendorBill.getLineCount({
sublistId: 'item'
});
log.debug('vendorBillLinesCount: ', vendorBillLinesCount);
for (var i = 0; i < vendorBillLinesCount; i++) {
newVendorBill.setSublistValue({
sublistId: 'item',
fieldId: 'cseg1',
line: i,
value: headerFinLoc
});
}
}
} catch (e) {
log.error({
title: 'Error',
details: e
});
}
}
return {
beforeLoad: beforeLoad
};
});
w
You have no lines on a new record. So the line count is zero and your for-loop never runs.
l
The Vendor Bill is being created from the PO. So there are lines upon creation. The no. of lines is also being logged correctly.
m
i would say try the following options 1. try setting it dynamicall using selectLine and setCurrentSublistValue and then commitline 2. instead of a user event to achieve this i would typically try this in a Client side script unless this need for other non UI Context ( For other UI context try doing this logic in the beforesubmit may be )
a
w
@Anthony OConnor and the row after that?
a
he's not creating the record in the script, i see no record.create ?
he's taking it from the context
w
I interpreted that line as it was OK to do manipulations in the context CREATE
a
yeah i agree its confusing, and poorly worded, I've always done this type of stuff in the pageinit or fieldchange of a client script
w
But a transform from a PO might not be considered a "real" create 🤷‍♂️
a
hmm I guess you're right... and line level vs. header might be different too? Like I said I don't recall ever doing something like this in a beforeLoad so I don't have any experience with it.
thinking about it though it makes sense, if its a create context then its not saved to the DB yet so there shouldnt be an issue with editing it
b
if what you are accomplishing can be done via a default value, then you can use beforeLoad to set the value
if you cant, it wont work
in general, setting sublist values in a before load is especially bad, doing so doesnt actually set any sourced values
👍 1
l
Thanks all. I'll try to convert this to pageInit and see what happens.