Custom GL Plugin script to Reverse the GL Posted.

function customizeGlImpact(transactionRecord, standardLines, customLines, book) {
    var recType = transactionRecord.getRecordType().toUpperCase();
    nlapiLogExecution('DEBUG', 'Record Type', recType);


    var lineCount = transactionRecord.getLineItemCount('item');
    nlapiLogExecution('DEBUG', 'Line Count', lineCount);


    var defaultAccountId = 'YOUR_DEFAULT_ACCOUNT_ID'; // Replace with your actual default account ID


    if (recType === 'VENDORBILL' || recType === 'VENDORCREDIT') {
        for (var i = 1; i <= lineCount; i++) {
            var isBillable = transactionRecord.getLineItemValue('item', 'isbillable', i);
            var lineAmount = parseFloat(transactionRecord.getLineItemValue('item', 'amount', i));
            var lineMemo = transactionRecord.getLineItemValue('item', 'description', i);
            var itemId = transactionRecord.getLineItemValue('item', 'item', i);
            var lineEntity = transactionRecord.getLineItemValue('item', 'customer', i);
            var lineDepartmentId = transactionRecord.getLineItemValue('item', 'department', i);
            var lineClassId = transactionRecord.getLineItemValue('item', 'class', i);
            var lineLocationId = transactionRecord.getLineItemValue('item', 'location', i);
            var lineAccountId = transactionRecord.getLineItemValue('item', 'account', i);


            // Log key variables for debugging
            nlapiLogExecution('DEBUG', 'Line Variables', JSON.stringify({
                isBillable: isBillable,
                lineAmount: lineAmount,
                lineMemo: lineMemo,
                itemId: itemId,
                lineEntity: lineEntity,
                lineDepartmentId: lineDepartmentId,
                lineClassId: lineClassId,
                lineLocationId: lineLocationId,
                lineAccountId: lineAccountId
            }));


            // Retrieve the account associated with the item if not already available
            if (!lineAccountId) {
                try {
                    var itemLookup = nlapiLookupField('item', itemId, 'expenseaccount');
                    lineAccountId = itemLookup || defaultAccountId;
                } catch (e) {
                    nlapiLogExecution('ERROR', 'Error Retrieving Item Account', e.message);
                    continue;
                }
            }


            // Skip lines with missing account IDs if no default account is set
            if (!lineAccountId) {
                nlapiLogExecution('ERROR', 'Missing Line Account', 'Line account ID is missing for line ' + i);
                continue;
            }


            // If isBillable is either F or null, perform the custom GL impact
            if (isBillable === 'F' || isBillable === null) {
                try {
                    // Load the item record to get the alternate expense account
                    var itemRecord = nlapiLoadRecord('serviceitem', itemId);
                    var alternateExpenseAccount = itemRecord.getFieldValue('custitem_xxf_alt_exp_acc');


                    if (alternateExpenseAccount) {
                        if (recType === 'VENDORBILL') {
                            // Reverse the booking of the line item amount for Vendor Bill
                            var reversalLine = customLines.addNewLine();
                            reversalLine.setAccountId(parseInt(lineAccountId));
                            reversalLine.setCreditAmount(lineAmount);
                            reversalLine.setMemo(lineMemo);
                            if (lineEntity) reversalLine.setEntityId(parseInt(lineEntity));
                            if (lineDepartmentId) reversalLine.setDepartmentId(parseInt(lineDepartmentId));
                            if (lineClassId) reversalLine.setClassId(parseInt(lineClassId));
                            if (lineLocationId) reversalLine.setLocationId(parseInt(lineLocationId));


                            // Book the amount against the alternate expense account
                            var alternateLine = customLines.addNewLine();
                            alternateLine.setAccountId(parseInt(alternateExpenseAccount));
                            alternateLine.setDebitAmount(lineAmount);
                            alternateLine.setMemo(lineMemo);
                            if (lineEntity) alternateLine.setEntityId(parseInt(lineEntity));
                            if (lineDepartmentId) alternateLine.setDepartmentId(parseInt(lineDepartmentId));
                            if (lineClassId) alternateLine.setClassId(parseInt(lineClassId));
                            if (lineLocationId) alternateLine.setLocationId(parseInt(lineLocationId));
                        } else if (recType === 'VENDORCREDIT') {
                            // Reverse the booking of the line item amount for Vendor Credit
                            var reversalLine = customLines.addNewLine();
                            reversalLine.setAccountId(parseInt(lineAccountId));
                            reversalLine.setDebitAmount(lineAmount);
                            reversalLine.setMemo(lineMemo);
                            if (lineEntity) reversalLine.setEntityId(parseInt(lineEntity));
                            if (lineDepartmentId) reversalLine.setDepartmentId(parseInt(lineDepartmentId));
                            if (lineClassId) reversalLine.setClassId(parseInt(lineClassId));
                            if (lineLocationId) reversalLine.setLocationId(parseInt(lineLocationId));


                            // Book the amount against the alternate expense account
                            var alternateLine = customLines.addNewLine();
                            alternateLine.setAccountId(parseInt(alternateExpenseAccount));
                            alternateLine.setCreditAmount(lineAmount);
                            alternateLine.setMemo(lineMemo);
                            if (lineEntity) alternateLine.setEntityId(parseInt(lineEntity));
                            if (lineDepartmentId) alternateLine.setDepartmentId(parseInt(lineDepartmentId));
                            if (lineClassId) alternateLine.setClassId(parseInt(lineClassId));
                            if (lineLocationId) alternateLine.setLocationId(parseInt(lineLocationId));
                        }
                    } else {
                        nlapiLogExecution('ERROR', 'No Alternate Expense Account', 'No alternate expense account found for item: ' + itemId);
                    }
                } catch (e) {
                    nlapiLogExecution('ERROR', 'Error in GL Impact Script', e.message);
                }
            }
        }
    } if (recType === "EXPENSEREPORT") {
        var expLineCount = transactionRecord.getLineItemCount('expense');
        nlapiLogExecution('DEBUG', 'Exp Line Count', expLineCount);
        for (var i = 1; i <= expLineCount; i++) {
            var isExpBillable = transactionRecord.getLineItemValue('expense', 'isbillable', i);
            var expLineAmount = parseFloat(transactionRecord.getLineItemValue('expense', 'amount', i));
            var expLineMemo = transactionRecord.getLineItemValue('expense', 'memo', i);
            var expCategory = transactionRecord.getLineItemValue('expense', 'category', i);
            var expLineEntity = transactionRecord.getLineItemValue('expense', 'customer', i);
            var expLineDepartmentId = transactionRecord.getLineItemValue('expense', 'department', i);
            var expLineClassId = transactionRecord.getLineItemValue('expense', 'class', i);
            var expLineLocationId = transactionRecord.getLineItemValue('expense', 'location', i);
            var expLineAccountId = transactionRecord.getLineItemValue('expense', 'expenseaccount', i);


            // Log key variables for debugging
            nlapiLogExecution('DEBUG', 'Line Variables', JSON.stringify({
                isExpBillable: isExpBillable,
                expLineAmount: expLineAmount,
                expLineMemo: expLineMemo,
                expCategory: expCategory,
                expLineEntity: expLineEntity,
                expLineDepartmentId: expLineDepartmentId,
                expLineClassId: expLineClassId,
                expLineLocationId: expLineLocationId,
                expLineAccountId: expLineAccountId
            }));


            // Retrieve the account associated with the expense category if not already available
            if (!expLineAccountId) {
                try {
                    var expLookup = nlapiLookupField('expensecategory', expCategory, 'expenseacct');
                    expLineAccountId = expLookup || defaultAccountId;
                } catch (e) {
                    nlapiLogExecution('ERROR', 'Error Retrieving Item Account', e.message);
                    continue;
                }
            }


            // Skip lines with missing account IDs if no default account is set
            if (!expLineAccountId) {
                nlapiLogExecution('ERROR', 'Missing Line Account', 'Line account ID is missing for line ' + i);
                continue;
            }




            // If isBillable is either F or null, perform the custom GL impact
            if (isExpBillable === 'F' || isExpBillable === null) {
                nlapiLogExecution('DEBUG', 'isBillable', isBillable);
                try {
                    // Load the exp category record to get the direct expense account
                    var expItemLookUp = nlapiLookupField('expensecategory', expCategory, 'expenseitem');
                    nlapiLogExecution('DEBUG', 'expItemLookUp', expItemLookUp);
                    var directExpenseAccount = nlapiLookupField('expenseitem', expItemLookUp, 'custitem_xxf_alt_exp_acc');
                    nlapiLogExecution('DEBUG', 'directExpenseAccount', directExpenseAccount);
                    //var expCategoryRecord = nlapiLoadRecord('expensecategory', expCategory);
                    //var directExpenseAccount = expCategoryRecord.getFieldValue('custrecord_xxf_expcat_directexp');


                    if (directExpenseAccount) {
                        // Reverse the booking of the line item amount for Expense Report
                        var reversalLine = customLines.addNewLine();
                        reversalLine.setAccountId(parseInt(expLineAccountId));
                        reversalLine.setCreditAmount(expLineAmount);
                        reversalLine.setMemo(expLineMemo);
                        if (expLineEntity) reversalLine.setEntityId(parseInt(expLineEntity));
                        if (expLineDepartmentId) reversalLine.setDepartmentId(parseInt(expLineDepartmentId));
                        if (expLineClassId) reversalLine.setClassId(parseInt(expLineClassId));
                        if (expLineLocationId) reversalLine.setLocationId(parseInt(expLineLocationId));


                        // Book the amount against the Direct expense account
                        var alternateLine = customLines.addNewLine();
                        alternateLine.setAccountId(parseInt(directExpenseAccount));
                        alternateLine.setDebitAmount(expLineAmount);
                        alternateLine.setMemo(expLineMemo);
                        if (expLineEntity) alternateLine.setEntityId(parseInt(expLineEntity));
                        if (expLineDepartmentId) alternateLine.setDepartmentId(parseInt(expLineDepartmentId));
                        if (expLineClassId) alternateLine.setClassId(parseInt(expLineClassId));
                        if (expLineLocationId) alternateLine.setLocationId(parseInt(expLineLocationId));
                    } else {
                        nlapiLogExecution('ERROR', 'No Direct Expense Account', 'No Direct expense account found for item: ' + expCategory);
                    }


                } catch (e) {
                    nlapiLogExecution('ERROR', 'Error in GL Impact Script', e.message);
                }
            }




        }




    }
}

Leave a comment

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