Adding a New Line to the Item Sublist in beforeSubmit User Event Script

.

🔍 Applicable Context

  • Script Type: User Event Script
  • Execution Context: beforeSubmit
  • Record Type: Any record with an item sublist (e.g., Sales Order, Invoice)
  • Use Case: Automatically inserting a default item line before the record is submitted

const beforeSubmit = (scriptContext) => {
    try {
        if (scriptContext.type !== scriptContext.UserEventType.CREATE) return;
        let newRecord = scriptContext.newRecord;
        let lineCount = newRecord.getLineCount({ sublistId: 'item' });

        const ITEM_ID = 1234; // Replace with valid internal ID of your item
        const AMOUNT = 250; // Desired amount for this line

        // Insert a new line at the end of the item sublist
        newRecord.insertLine({
            sublistId: 'item',
            line: lineCount
        });

        // Set item and amount for the new line
        newRecord.setSublistValue({
            sublistId: 'item',
            fieldId: 'item',
            line: lineCount,
            value: ITEM_ID
        });
        newRecord.setSublistValue({
            sublistId: 'item',
            fieldId: 'amount',
            line: lineCount,
            value: AMOUNT
        });
    } catch (err) {
        log.error('Error @ beforeSubmit', err);
    }
}

Leave a comment

Your email address will not be published. Required fields are marked *