Journal Approval Routing

XSEED Client aims to implement a Journal Entry Approval workflow in NetSuite by which if a journal entry (JE) is created or modified, the approver should be notified, and the changes will only impact the General Ledger (GL) after the approver grants approval.

By standard, there are no possibility to take the line level fields in the journal entry by workflow. A workflow action script trigger and line level fields which makes GL impact will fetch. The Workflow action script return 1 or 0. If it return the 1, the fields are modified and the workflow trigger and status set to Pending approval.

We will create a workflow field to store the returned value (1 or 0)

Workflow action script

/**
 * @NApiVersion 2.1
 * @NScriptType WorkflowActionScript
 */
/*************************************************************************************
 ***********
 * XSEED Education Pte Ltd​-IND-NS
 *
 * XSEED-528 : Journal Entry Approval Routing
 *
 *
 *************************************************************************************
 ***********
 *
 *
 * Author: Jobin and Jismi IT Services LLP
 *
 * Date Created : 15-September-2023
 *
 * Description: Script used to set the approval status to Pending approval when the line level fields updated.
 *
 * REVISION HISTORY
 *
 * @version 1.0 XSEED-528 : 15-September-2023 : Created the initial build by JJ0125
 *
 *
 *************************************************************************************
 **********/
define([],

    () => {
        /**
         * Defines the WorkflowAction script trigger point.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.workflowId - Internal ID of workflow which triggered this action
         * @param {string} scriptContext.type - Event type
         * @param {Form} scriptContext.form - Current form that the script uses to interact with the record
         * @since 2016.17
         */
        const onAction = (scriptContext) => {
            try {

                if (scriptContext.type !== "edit") {
                    return;
                }
                
                let newRecord = scriptContext.newRecord;
                let oldRecord = scriptContext.oldRecord;            
    
                var oldLineItems = [];
                var newLineItems = [];

                // Get the line item counts for both old and new records
                var oldLineCount = oldRecord.getLineCount({ sublistId: 'line' });
                log.debug("oldRecLineCount", oldLineCount);
                var newLineCount = newRecord.getLineCount({ sublistId: 'line' });
                log.debug("newRecLineCount", newLineCount);
                log.debug("condition check", Math.max(oldLineCount, newLineCount));
                // Loop through line items and create objects for each
                for (var i = 0; i < Math.max(oldLineCount, newLineCount); i++) {
                    oldLineItems.push({
                        account: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'account', line: i }),
                        debit: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'debit', line: i }),
                        credit: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'credit', line: i }),
                        grossamt: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'grossamt', line: i }),
                        taxcode: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'taxcode', line: i }),
                        taxrate1: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'taxrate1', line: i }),
                        entity: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'entity', line: i }),
                        costcenter: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'department', line: i }),
                        class: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'class', line: i }),
                        location: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'location', line: i }),
                        academic: oldRecord.getSublistValue({ sublistId: 'line', fieldId: 'custcol_cseg_academic_cycle', line: i }),
                    });

                    newLineItems.push({
                        account: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'account', line: i }),
                        debit: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'debit', line: i }),
                        credit: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'credit', line: i }),
                        grossamt: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'grossamt', line: i }),
                        taxcode: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'taxcode', line: i }),
                        taxrate1: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'taxrate1', line: i }),
                        entity: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'entity', line: i }),
                        costcenter: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'department', line: i }),
                        class: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'class', line: i }),
                        location: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'location', line: i }),
                        academic: newRecord.getSublistValue({ sublistId: 'line', fieldId: 'custcol_cseg_academic_cycle', line: i }),
                    });
                }
                log.debug("oldLineItems", oldLineItems);
                log.debug("newLineItems", newLineItems);

                // Compare the old and new arrays of line items as objects
                if (JSON.stringify(oldLineItems) !== JSON.stringify(newLineItems)) {
                    log.debug('Line Item Changes Detected', 'Journal Entry line item changes detected.');
                    return 1;
                }
                else {
                    return 0;
                }

            } catch (e) {
                log.error("error @main function", e);
            }

        }

        return { onAction };
    });

Leave a comment

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