Hi, I am creating sales orders with a map/reduce s...
# suitescript
s
Hi, I am creating sales orders with a map/reduce script and running into a problem when the SO form has enable item line shipping checked. It works if using the default address, however, I am trying to insert a new address using my data. The error I am getting is Please choose an item to add. In my code, I first create the sales order, then I go through and create the lines (dynamic mode, if it matters). I THEN try to add the address to each line, and when I try and commit the line, that is when the error occurs. I did try adding the address to the lines as I was creating them, but it does seem that the line has to be committed first before an address can be added. Any idea what is causing that error message?
b
what does the code look like
s
I can start with the function that creates the line level address after the sales order and the lines have been created/committed:
b
the ideal amount of code is enough code to reproduce the problem
s
Copy code
/**
         * Sets line level ship to addresses on Netsuite SO
         *
         * @param {SalesOrder} salesOrder the Netsuite SalesOrder record
         * @param {Object} lbAddress My address data to populate it with
         */
        function setLineLevelShipTo(salesOrder, lbAddress) {
            var lineCount = salesOrder.getLineCount({ sublistId: 'item' });

            salesOrder.selectLine({ sublistId: 'item', line: 0 });
            
            var addr = salesOrder.getCurrentSublistSubrecord({ sublistId: 'item', fieldId: 'shippingaddress' });

            // Get all values
            var attention = (lbAddress.CompanyName ? lbAddress.CompanyName : '');
            var addressee = (lbAddress.FirstName ? lbAddress.FirstName + ' ' : '') + (lbAddress.LastName ? lbAddress.LastName : '');
            var addr1 = lbAddress.Address1 ? lbAddress.Address1 : '';
            var addr2 = lbAddress.Address2 ? lbAddress.Address2 : '';
            var city = lbAddress.City ? lbAddress.City : '';
            var state = lbAddress.State ? lbAddress.State : '';
            var zip = lbAddress.Zip ? lbAddress.Zip : '';
            var phone = lbAddress.Phone ? lbAddress.Phone : '';

            addr.setValue({
                fieldId: 'country',
                value: lbAddress.Country ? lbAddress.Country : 'US'
            });
            addr.setValue({ fieldId: 'attention', value: attention });
            addr.setValue({ fieldId: 'addressee', value: addressee });
            addr.setValue({ fieldId: 'addr1', value: addr1 });
            addr.setValue({ fieldId: 'addr2', value: addr2 });
            addr.setValue({ fieldId: 'city', value: city });
            addr.setValue({ fieldId: 'state', value: state });
            addr.setValue({ fieldId: 'zip', value: zip });
            addr.setValue({ fieldId: 'addrphone', value: phone });
            addr.commit(); // I have tried it with and without this line, no change
            salesOrder.commitLine({ sublistId: 'item' }); // This is the line it fails on

            var lineShipAddr = salesOrder.getSublistValue({
                sublistId: 'item',
                fieldId: 'shipaddress',
                line: 0
            });

            for (var i = 1; i < lineCount; i++) {
                salesOrder.selectLine({ sublistId: 'item', line: i });
                salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'shipaddress', value: lineShipAddr });
                salesOrder.commitLine({ sublistId: 'item' });
            }
        }
b
ideally the minimal amount to do so
s
ok here is that main problem funciton, i can try and clean up code enough to give a minimum of what happens before this step
I also commented the line it fails on
Hold on, trying to create simplified code might have found the issue... Creating the lines might come after trying to set the line's address 🙈 don't waste your time looking at this yet
Wow thank you rubber ducky rubber duck debugging I guess I just needed to simplify down my code to find the problem, its so bloated
Believe it or not I only post here after I'm at my wits end after hours of trying everything. But yea I guess my code was correct just in the wrong order and I failed to understand the error message. Thanks 🙂