Hi All. In a sales order, I'm trying to add an add...
# suitescript
j
Hi All. In a sales order, I'm trying to add an additional item to the item list when adding a specific item. I have created a validateLine script that gets triggered when an item is entered. It seems to get called correctly. I have logic in place that if the item on the line equals a specific item, it will then call selectNewLine, populate the item i wish to add, then call commitLine. No matter what i do, it seems to only replace the original item with the new one, instead of add both. Does anyone have a working model of how to do this? Any incite would be greatly appreciated. Thx
Copy code
/**
     * @NApiVersion 2.x
     * @NScriptType ClientScript
     */
    define(['N/record', 'N/ui/dialog', 'N/log', 'N/error'], function(record, dialog, log, error) {
        
        function validateLine(scriptContext) {

            var currentRecord = scriptContext.currentRecord;
            var sublistId = scriptContext.sublistId;
               
            var myItem = currentRecord.getCurrentSublistValue({
                sublistId: sublistId,
                fieldId: 'item'
            });                
            
            if (myItem === '42343') {

                // If validation passes and a new line should be added
                try {

                     // Select a new line in the same sublist
                    currentRecord.selectNewLine({
                        sublistId: sublistId,
                    });

                    // Set values for the new line 
                    currentRecord.setCurrentSublistValue({
                        sublistId: sublistId,
                        fieldId: 'item', 
                        value: 52154
                    });

                    // ... set other field values as needed
                    currentRecord.setCurrentSublistValue({
                        sublistId: sublistId,
                        fieldId: 'location',
                        value: 14
                    });                

                    currentRecord.setCurrentSublistValue({
                        sublistId: sublistId,
                        fieldId: 'quantity',
                        value: 2
                    });                

                    // Commit the new line
                    currentRecord.commitLine({
                        sublistId: sublistId,
                    });

                } catch (e) {

                    // Handle any errors during line creation
                    log.error({
                        title: 'Error adding new line',
                        details: e.message
                    });
                }
            }

            // Always return true to allow the original line to be committed
            return true;
        }

        return {
            validateLine: validateLine
        };
    });
a
share your code not sure but my guess would be you're creating a new line in a validate line and committing that new line ALSO calls validate line, which ... NS probably detects and prevents somehow?
j
@Anthony OConnor Thanks for your input. At one point it was acting recursively and caught in a death loop... I added the code above. The strange thing is that although it appears to overwrite the original item entered, it also puts the price of the new item on the next line. See the image below. The item I am trying to append to the end of the list is in green. The price of $222, is the price I entered for the original item. The price below the green line (20.50) is the actually price of the item above. Almost seems like a timing issue or both items are competing against each other...
a
don't have a solution, but thinking through the process... you're essentially already inside a "select new line" type process while you're adding the current line in the UI so when you do select new line INSIDE that I'm not sure it can add a new line correctly... I think select new line essentially does create newline with line value = currentSublist.lineCount so you're on line 3 (2 with zero index) (green line) but that line hasn't been saved yet so your sublist line count is only 2 so the line for your select new line is ALSO targeting line 3(index 2)
j
You might be right. I saw this solution from a google search, so who knows how valid it is... In my actual code, i have a bunch of debugging lines and one thing i literally just tested while i saw you were typing was to see the return value from the SelectNewLine call. It returns, "current record". If you are correct, any thoughts on a work around or different approach?
also, when i log the current line, it's always 2. But that price on line 3 is still baffling me!
a
yeah that's weird
so you can likely handle this with a different clinet script entry point sublistChanged triggers on line add/ line remove etc of a specific sublist
but you're not gonna have line context, so you'll have to loop thru all the sublist lines and get the item ids... AND also check for already existing add on lines you've created
essentially you'll need check for itemids of 123 and 456 next to each other and if they are that's ok.. .but if there's a 123 and no 456 after it then you need to add your 456 line
but idk what you'd do in a scenario where they add multiple 123s as different lines... do you add a 456 for each? or do you add only one 456 and increase the quantity? also have to consider the remove line scenario if they remove 123 line do you need to remove a 456 line?
or reduce qty of the 456 line by 1 if there's potentially >1
123 = the item you're checking for 456 = the item you're adding 🙂
j
ah! I didn't know about sublistChanged. I'm ok with that logic, I have written similar code recently. I'll look into that instead. I might also look into PostSourcingEntryBlock. Thanks so much for taking the time, much appreciated! I'll report back...
👍 1