Is it possible to loop through Items on a Sales Or...
# suitescript
e
Is it possible to loop through Items on a Sales Order and change the Location field on all of them? Whenever I try to loop, the first location updates but none of the others.
Copy code
var itemCount = salesOrder.getLineCount({
        sublistId: 'item'
    });
    var currentLocation = salesOrder.getValue({
        fieldId: 'location'
    });

    for (i = 0; i < itemCount; i++) {
        salesOrder.selectLine({
            sublistId: 'item',
            line: i
        });
        salesOrder.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'location',
            value: currentLocation,
        });
        salesOrder.commitLine({
            sublistId: 'item',
        })
    }
d
Copy code
record.setSublistValue({
            sublistId: 'item',
            fieldId: 'location',
            value: location,
            line: line
          });
This is the code I use, it doesn't have the selectLine and commitLine logic you have it sets the sublist value for the selected line in one method.
c
You don't need to select the line unless the record object is in dynamic mode as well.
e
Im sorry - I'm trying to do this client side. I think client side I have to user N/currentRecord and in that case, I do need to use selectLine / setCurrentSublistValue and commitLine (please correct me if I'm wrong)
c
I'd do it on beforeSubmit personally. It avoids having to deal w/ client side logic and it also works for imports should they ever do it.
The only reason(s) to do it client side is they need the location info immediately for some reason. The only thing client side should really be used for is validation IMO. Do the rest in a before or after submit unless it really warrants being client side.
💯 1
s
of course, I'd be writing this something simple like
_.forEach(salesOrder.item, line => line.location = somelocation)
b
setCurrentSublistValue is asynchronous, use forceSyncSourcing to avoid committing multiple lines at the same time
e
thank you @battk that was exactly what I was looking for