Adjusting the Tax Amount on the Invoice Item line

REQUIREMENT

Client would like to develop a script that should adjust the Tax Amount on the Invoice Item line if the Gross amount is within 0.01 of a whole integer.

So, this adjustment will round the Gross Amount to the nearest whole integer such that 19.99 ≈> 20 <≈ 20.01

Need to implement a script on the save of a new Invoice to adjust the Tax Amount when the mentioned scenario occurs with the line level Gross Amount.

SOLUTION

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/search'],
    /**
 * @param{record} record
 * @param{search} search
 */
    (record, search) => {
        /**
         * Defines the function definition that is executed before record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const beforeSubmit = (scriptContext) => {
            try {
                let contextType=scriptContext.type

               if(contextType == 'create'){
                    let invoiceRec = scriptContext.newRecord
                    log.debug("invoiceRec", invoiceRec)
                    let itemSublist = invoiceRec.getLineCount({
                        sublistId: 'item'
                    })
                    for (let items = 0; items < itemSublist; items++) {
                        let grossAmnt = invoiceRec.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'grossamt',
                            line: items
                        })

                        const roundedValue = Math.round(grossAmnt);
                        
                        const difference = roundedValue - (grossAmnt)

    
                        let roundedDiff = difference.toFixed(2)

                        let finalTaxAmount
    
                        if (Math.abs(roundedDiff) <= 0.01) {
                            let taxAmnt = invoiceRec.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'tax1amt',
                                line: items
                            })
                            log.debug("taxAmnt", taxAmnt)
                            if (roundedDiff > 0) {
                                log.debug(">0", roundedDiff)
                                finalTaxAmount = Number(taxAmnt) + Number(roundedDiff)
                            }
                            else {
                                log.debug("<0", roundedDiff)
                                finalTaxAmount = Number(taxAmnt) -   Number(Math.abs(roundedDiff))
                            }

                            invoiceRec.setSublistValue({
                                sublistId: 'item',
                                fieldId: 'tax1amt',
                                line: items,
                                value: finalTaxAmount
                            })
                        }
                    }
               }
                
            }
            catch (err) {
                log.error("error@beforeSubmit", err)
            }

        }


        return {  beforeSubmit }

    });

Leave a comment

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