User Event script to add 3% charge to the Invoice created.

We have to add additional charge (3% of total) to the total amount when we create invoice record. For that can use markup item. We have to add a markup item to the line items in the invoice with amount of 3% of total. The user event script will execute when invoice is created.

/* **********************************************************************************************
* UserEventScript for adding invoice charge
* **************************************************************************
* RIZE-275: Script to add 3% of total as charge in invoice
* **********************************************************************************************
*
* Author: Jobin and Jismi IT Services
*
* Date Created : 22-February-2024
*
* Description : This script is to add markup item with 3% charge of total to the invoice
*
* REVISION HISTORY
*
* @version 1.0 : 22-February-2024 : 
*
*
***********************************************************************************************
**/
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/


define(['N/record'], function (record) {
    function beforeSubmit(context) {
        try {
            if ((context.type !== context.UserEventType.CREATE)) {
                return false;
            } else {
                var invoiceRecord = context.newRecord;
                var markupItem = 0;
                var total = invoiceRecord.getValue({
                    fieldId: 'total'
                });
                //calculate 3% of the total 
                total = (total || 0) * 0.03;             
                var count = invoiceRecord.getLineCount({
                    sublistId: 'item'
                });
                for (var i = 1; i <= count; i++) {
                    var itemInList = invoiceRecord.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'item',
                        line: i
                    })
                    if (itemInList === 7022) {
                        markupItem++;
                    }
                }
                if (markupItem === 0) {
                    invoiceRecord.insertLine({
                        sublistId: 'item',
                        line: count
                    });
                    invoiceRecord.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'item',
                        line: count,
                        value: 7022 // Id of the markup item
                    });
                    invoiceRecord.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'price',
                        line: count,
                        value: -1
                    });
                    invoiceRecord.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'amount',
                        line: count,
                        value: total
                    });
                }
            }
        } catch (error) {
            log.debug("error @ beforesubmit", error);
        }
    }
    return {
        beforeSubmit: beforeSubmit
    };
});

Leave a comment

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