Amount Validation in Advanced Intercompany Journal Entry Workflow

Scenario

An approval workflow for journal entries is required, where validation is triggered only if the transaction amount is greater than or equal to 1,00,000 INR. There is an issue with retrieving the exchange rate at the body level. Although the exchange rate is displayed on the form, it is not visible in the record. The exchange rate needs to be retrieved from the body level to perform the calculation.

Solution

For Intercompany Journal Entries, the exchange rate will be displayed at the line level. As a result, we need to consider the total amount using the line-level exchange rate of each subsidiary section.

Userevent script:

define(['N/record', 'N/log', 'N/runtime'],
    /**
     * @param {record} record - The NetSuite record module
     * @param {log} log - The NetSuite log module
     * @param {runtime} runtime - The NetSuite runtime module
     */
    function(record, log, runtime) {
        /**
         * Function definition that is executed before a record is submitted.
         * @param {Object} context - Script execution context.
         * @param {Record} context.newRecord - New record being processed.
         * @param {Record} context.oldRecord - Old record, if applicable.
         * @param {string} context.type - Trigger type; use values from the context.UserEventType enum.
         * @since 2015.2
         */
        function beforeSubmit(context) {
            let contextType = context.type;

            if (contextType !== context.UserEventType.CREATE && contextType !== context.UserEventType.EDIT) {
                return;
            }

            try {
                let intercompanyJE = context.newRecord;
                let lineCount = intercompanyJE.getLineCount({
                    sublistId: 'line'
                });

                let totalCalculatedDebit = 0;

                for (let i = 0; i < lineCount; i++) {
                    let debitAmount = intercompanyJE.getSublistValue({
                        sublistId: 'line',
                        fieldId: 'debit',
                        line: i
                    });

                    let exchangeRate = intercompanyJE.getSublistValue({
                        sublistId: 'line',
                        fieldId: 'linefxrate', // Assuming 'exchangerate' is the correct field ID at the line level
                        line: i
                    });

                    log.debug({
                        title: 'Line ' + (i + 1),
                        details: 'Debit: ' + debitAmount + ', Exchange Rate: ' + exchangeRate
                    });

                    // Calculate the debit amount using the line-level exchange rate
                    let calculatedDebit = (debitAmount || 0) * (exchangeRate || 1);
                    totalCalculatedDebit += calculatedDebit;
                }

                log.debug({
                    title: 'Total Calculated Debit Amount',
                    details: totalCalculatedDebit
                });

                // Set the total calculated debit amount in the custom field before the record is saved
                intercompanyJE.setValue({
                    fieldId: 'custbody_jj_total_amount_rspdc_38',
                    value: totalCalculatedDebit
                });

                log.debug({
                    title: 'Total Calculated Debit Amount Set',
                    details: 'Field custbody_jj_total_amount_rspdc_38 set to ' + totalCalculatedDebit
                });
            } catch (error) {
                log.error({
                    title: 'Error in beforeSubmit',
                    details: error.message
                });
            }
        }

        return {
            beforeSubmit: beforeSubmit
        };
    }
);

Leave a comment

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