Any tricks to add multiple lines to a sublist quic...
# suitescript
w
Any tricks to add multiple lines to a sublist quick in client script? The sublist doesn't have any sourcing or other client scripts. It's just annoying to wait a couple of seconds for all the selectnewline and commit line to execute.
s
no, but make sure you’re setting β€˜`forcesyncsourcing: true`’ on
setValue()
w
I guess you mean on setCurrentSublistValue(). Would it speed up anything?
n
You can try SS 1.0, nlapiSetLineItemValue. You don't need to select new line and commit line. P.S. SS 1.0 code can be executed in SS 2.x based client script
w
I've used that when I want to update existing sublist lines with new values. But when I want to add lines I can't get that to work.
Copy code
console.time('loopy')
//const queryResult = [
//    {fieldId1: value, fieldId2: value}
//]
queryResult.forEach((result, index) => {
//currentRecord.selectNewLine({ sublistId });
    Object.entries(result).forEach(([fieldId, value]) => {
        nlapiSetLineItemValue(sublistId, fieldId, index+1, value)
        //currentRecord.setCurrentSublistValue({ sublistId, fieldId, value, ignoreFieldChange: true });
    });
 //currentRecord.commitLine({ sublistId });
});
console.timeEnd('loopy')
n
Where does index come from?
w
from the queryResult.forEach-loop on line 5.
n
I suggested to use nlapiSetLineItemValue without the select new line and commit line.
w
Yes, notice that the selectNewLine and commitLine is commented out.
(and the setCurrentSublistValue should've been commented out as well)
n
Sorry, on my phone it is not displaying correctly.
w
For the adventurous of you... from 3 seconds -> 50ms
Copy code
const recmach = getMachine(sublistId)
        recmach.insertdata([[...the_data].join(String.fromCharCode(1))].join(String.fromCharCode(2)),0)
        recmach.recalc()
        recmach.buildtable()
You can get how
the_data
should look like with recmach.getLineArray()
😲 1
n
Copy code
nlapiSetLineItemValue(sublistId, fieldId, index+1, value)
See if this works independently - if you still want to use this.
w
Can also get the order of the fields with
recmach.getFieldIdxMap()
Copy code
nlapiSetLineItemValue(sublistId, fieldId, index+1, value)
doesn't do anything when executed it in the console.
n
Did you pass in the values?
w
πŸ™‚ yes
n
That's weird..
w
It works to update an existing line, but I cannot add more lines.
n
which record are you on?
w
It's a custom record that has a child record.
n
and how are you getting the index?
w
I run a query to get what values I need to fill in. That query returns an array of objects. The index is the array-index that I loop through.
n
No, I mean when you ran it in the console.
w
ah, then I tested on line "1" and "2".
(on an empty sublist)
n
Yeah, I tried it on the sales order right now. You are right, it's not adding a new line. May be I have used it with existing line.
nlapiSetCurrentLineItemValue worked.
It seems we do need selectLine and commitLine regardless of SS version
w
unless going the hackery route. πŸ™‚
n
Yeah, thanks for this:
Copy code
const recmach = getMachine(sublistId)
        recmach.insertdata([[...the_data].join(String.fromCharCode(1))].join(String.fromCharCode(2)),0)
        recmach.recalc()
        recmach.buildtable()
w
haha, you shouldn't thank. It will probably cause some serious problems some time. 😈
πŸ˜† 3
I've kept the standard versions in code for when someone needs to roll back πŸ˜„
πŸ‘ 1
s
I've had issues with setting values in client scripts when forcesyncsourcing wasn't used.
t
Can I get the complete code on this one πŸ˜… I am having the same issue, but this time I cannot enter a new line and the other column can't be populated
w
I didn't have any issues, just that it wasn't as fast as I wanted it.
t
Ah I for some reason I thought there has been an issue on setting an initial value..