Amount After Discount in Invoice Record

JIRA CODE : [YALLA-52] Amount after discount in Invoice record – JIRA – Jobin & Jismi IT Services LLP (atlassian.net)

Description

Create a script for the Invoice record and when the user adds any discount for an item on the creation of the Invoice record, the script will populate the value in the “Amount after discount” field.

Solution

Created a User Event script for the Invoice record. The script will trigger after the saving the invoice record. We have created a line item field for the Discount Amount in the Invoice record. If the user specifies a discount amount then it is deducted from the gross amount and populate this value to the Amount after discount field. If the user do not give any discount amount to the particular field the Amount after discount field populate the Gross amount itself.

/**
 * @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 after 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 afterSubmit = (scriptContext) => {

            try{

                let currentRec = scriptContext.newRecord;
                let curRec=record.load({
                    type: record.Type.INVOICE,
                    id: currentRec.id
                });
                let lineCount = currentRec.getLineCount({
                    sublistId: 'item'
                });
                log.debug("Line Count", lineCount);
                for( var i = 0 ; i < lineCount; i++){
                    let amount = curRec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'grossamt',
                        line: i
                    });
                    log.debug("amount",amount)
                    let discountAmount = curRec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_jj_discount_amount_yalla_52',
                        line: i
                    });
                    let amountAfterDiscount;
                    if(discountAmount)
                    {
                        amountAfterDiscount =(amount - discountAmount);
                       
                    }
                    else
                    {
                        amountAfterDiscount = amount;
                       
                    }

                    let finalAmount = curRec.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_jj_discount_amount',
                        line: i,
                        value:amountAfterDiscount
                    });

                }
                curRec.save({
                    enableSourcing: true,
                    ignoreMandatoryFields: true
                })

            }
            catch(err)
            {
                log.debug("error@AfterSubmit",err)
            }

        }

        return { afterSubmit}

    });

Leave a comment

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