From 1a5076f092dc7f94a05d903d9b650a81eeb75d2c Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Thu, 24 Oct 2024 19:48:41 +0530 Subject: [PATCH] Add audit logs --- .../create_credit_card_charge.js | 76 ++++++++++++++----- .../create_credit_card_refund.js | 46 ++++++++++- 2 files changed, 100 insertions(+), 22 deletions(-) diff --git a/credit_card_charge_restlet/create_credit_card_charge.js b/credit_card_charge_restlet/create_credit_card_charge.js index 259ae93..0faf941 100644 --- a/credit_card_charge_restlet/create_credit_card_charge.js +++ b/credit_card_charge_restlet/create_credit_card_charge.js @@ -3,7 +3,7 @@ * @NScriptType Restlet * @NModuleScope Public */ -define(['N/record'], function (record) { +define(['N/record', 'N/search'], function (record, search) { // Function called upon sending a POST request to create a new credit card charge record function doPost(requestBody) { @@ -17,22 +17,61 @@ define(['N/record'], function (record) { } } - // Function called upon sending a GET request to retrieve a record + // Function called upon sending a GET request to retrieve a record and system notes function doGet(requestParams) { try { - // Load the credit card charge record in dynamic mode var creditCardCharge = record.load({ type: record.Type.CREDIT_CARD_CHARGE, id: requestParams.internalId, isDynamic: true }); - - return creditCardCharge; + + var systemNotes = getSystemNotes(requestParams); + + return { + creditCardCharge: creditCardCharge, + systemNotes: systemNotes + } } catch (e) { log.error('Error in doGet', e); return { success: false, message: e.message }; } - } + } + + // Function to get system notes for a specific record + function getSystemNotes(params) { + var systemNotesSearch = search.create({ + type: record.Type.CREDIT_CARD_CHARGE, // Adjust based on record type, could be 'customer', etc. + filters: [ + ['internalid', 'is', params.internalId], + 'AND', + ['systemnotes.date', 'onorafter', params.fromDate || '1/1/2018'] // Adjust default date as needed + ], + columns: [ + search.createColumn({ name: 'date', join: 'systemNotes' }), + search.createColumn({ name: 'field', join: 'systemNotes' }), + search.createColumn({ name: 'newvalue', join: 'systemNotes' }), + search.createColumn({ name: 'oldvalue', join: 'systemNotes' }), + search.createColumn({ name: 'context', join: 'systemNotes' }), + search.createColumn({ name: 'name', join: 'systemNotes' }) // Correct column for 'set by' + ] + }); + + var searchResults = []; + systemNotesSearch.run().each(function (result) { + searchResults.push({ + Date: result.getValue({ name: 'date', join: 'systemNotes' }), + Field: result.getValue({ name: 'field', join: 'systemNotes' }), + NewValue: result.getValue({ name: 'newvalue', join: 'systemNotes' }), + OldValue: result.getValue({ name: 'oldvalue', join: 'systemNotes' }), + Context: result.getValue({ name: 'context', join: 'systemNotes' }), + SetBy: result.getText({ name: 'name', join: 'systemNotes' }) // Use 'name' for the user who made the change + }); + return true; // Continue the search + }); + + return searchResults; + } // Function called upon sending a PUT request to update a record function doPut(requestBody) { @@ -42,7 +81,7 @@ define(['N/record'], function (record) { id: requestBody.internalId, isDynamic: true }); - + if (requestBody.entity) { creditCardCharge.setValue({ fieldId: 'entity', value: requestBody.entity.internalId }); } if (requestBody.account) { creditCardCharge.setValue({ fieldId: 'account', value: requestBody.account.internalId }); } if (requestBody.tranDate) {creditCardCharge.setValue({ fieldId: 'trandate', value: new Date(requestBody.tranDate) }); } @@ -70,61 +109,61 @@ define(['N/record'], function (record) { fieldId: 'account', value: expense.account.internalId }); - + creditCardCharge.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'amount', value: expense.amount }); - + creditCardCharge.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'memo', value: expense.memo }); - + creditCardCharge.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'department', value: expense.department.internalId }); - + creditCardCharge.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'class', value: expense.class.internalId }); - + creditCardCharge.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'location', value: expense.location.internalId }); - + creditCardCharge.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'customer', value: expense.customer.internalId }); - + creditCardCharge.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'isbillable', value: expense.isBillable }); - + creditCardCharge.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'taxcode', value: expense.taxCode.internalId }); - + creditCardCharge.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'taxamount', value: expense.taxAmount }); - + expense.customFieldList.forEach(function (customField) { creditCardCharge.setCurrentSublistValue({ sublistId: 'expense', @@ -170,8 +209,7 @@ define(['N/record'], function (record) { sublistId: 'expense' }); - - var expenses = requestBody.expenses + var expenses = requestBody.expenses; expenses.forEach(function (expense) { creditCardCharge.setCurrentSublistValue({ diff --git a/credit_card_refund_restlet/create_credit_card_refund.js b/credit_card_refund_restlet/create_credit_card_refund.js index f6d5f4c..62c2960 100644 --- a/credit_card_refund_restlet/create_credit_card_refund.js +++ b/credit_card_refund_restlet/create_credit_card_refund.js @@ -3,7 +3,7 @@ * @NScriptType Restlet * @NModuleScope Public */ -define(['N/record'], function (record) { +define(['N/record', 'N/search'], function (record, search) { // Function called upon sending a POST request to create a new credit card refund record function doPost(requestBody) { @@ -17,7 +17,7 @@ define(['N/record'], function (record) { } } - // Function called upon sending a GET request to retrieve a record + // Function called upon sending a GET request to retrieve a record and system notes function doGet(requestParams) { try { // Load the credit card refund record in dynamic mode @@ -27,13 +27,53 @@ define(['N/record'], function (record) { isDynamic: true }); - return creditCardRefund; + var systemNotes = getSystemNotes(requestParams); + + return { + creditCardCharge: creditCardRefund, + systemNotes: systemNotes + } } catch (e) { log.error('Error in doGet', e); return { success: false, message: e.message }; } } + // Function to get system notes for a specific record + function getSystemNotes(params) { + var systemNotesSearch = search.create({ + type: record.Type.CREDIT_CARD_REFUND, // Adjust based on record type, could be 'customer', etc. + filters: [ + ['internalid', 'is', params.internalId], + 'AND', + ['systemnotes.date', 'onorafter', params.fromDate || '1/1/2018'] // Adjust default date as needed + ], + columns: [ + search.createColumn({ name: 'date', join: 'systemNotes' }), + search.createColumn({ name: 'field', join: 'systemNotes' }), + search.createColumn({ name: 'newvalue', join: 'systemNotes' }), + search.createColumn({ name: 'oldvalue', join: 'systemNotes' }), + search.createColumn({ name: 'context', join: 'systemNotes' }), + search.createColumn({ name: 'name', join: 'systemNotes' }) // Correct column for 'set by' + ] + }); + + var searchResults = []; + systemNotesSearch.run().each(function (result) { + searchResults.push({ + Date: result.getValue({ name: 'date', join: 'systemNotes' }), + Field: result.getValue({ name: 'field', join: 'systemNotes' }), + NewValue: result.getValue({ name: 'newvalue', join: 'systemNotes' }), + OldValue: result.getValue({ name: 'oldvalue', join: 'systemNotes' }), + Context: result.getValue({ name: 'context', join: 'systemNotes' }), + SetBy: result.getText({ name: 'name', join: 'systemNotes' }) // Use 'name' for the user who made the change + }); + return true; // Continue the search + }); + + return searchResults; + } + // Function called upon sending a PUT request to update a record function doPut(requestBody) { try {