how to set item lines in user event aftersubmit
# ask-stanley-ai
n
how to set item lines in user event aftersubmit
c
AI generated answer (using internal Slack threads):
To set item lines in a User Event afterSubmit, you need to load the record in your script, then save the record after setting values. Here is a sample code snippet:
Copy code
javascript
function afterSubmit(context) {
    try{
        var newRecordID = context.newRecord.id;
        var invoiceRecord = record.load({
            type : 'invoice',
            id   : newRecordID,
            isDynamic : true
        });
        var lineCount = invoiceRecord.getLineCount('item');
        for( var i = 0; i < lineCount; i += 2 ){
            var item = invoiceRecord.getSublistValue({
                sublistId : 'item',
                fieldId   : 'item',
                line      : i
            });

            if (item !== '6177'){
                var quantity = invoiceRecord.getSublistValue({
                    sublistId : 'item',
                    fieldId   : 'quantity',
                    line      : i
                });

                var item = invoiceRecord.getSublistValue({
                    sublistId : 'item',
                    fieldId   : 'item',
                    line      : i + 1
                });

                if(item === '6177'){
                    invoiceRecord.setSublistValue({
                        sublistId : 'item',
                        fieldId   : 'quantity',
                        line      : i + 1,
                        value     : quantity
                    });
                }
            }
        }
        invoiceRecord.save();
    }catch(e){log.debug('error',e);}
}
In this example, the script is loading an invoice record in dynamic mode, getting the line count of the 'item' sublist, and then looping through each line. If the item on the line is not '6177', it gets the quantity of the item and then checks the next line. If the item on the next line is '6177', it sets the quantity of that line to the quantity of the previous line. The record is then saved. Remember, when working in dynamic mode, you need to use dynamic mode methods when getting/setting sublist line values. These include
selectLine
,
getCurrentSublistValue
,
setCurrentSublistValue
, and
commitLine
. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.831535757
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.825992882
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.824927568
Please provide your feedback on this answer.