'Generating Amortization Journal Entry Memo Lines
i'm currently trying to run this script:
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/query'],
function (record, query) {
function afterSubmit(context) {
// We need to run this in afterSubmit as it appears from tests that the schedulenum field
// was blank/uninitialized in the beforeSubmit hook when the JE is created (i.e. context.type = 'create').
// That could be an issue if JEs are set up to automatically post. As such, we use afterSubmit to avoid any problem.
if (context.type === 'delete') {
return;
}
var rec = context.newRecord;
if (rec.getValue('approvalstatus') === '2' /*= Approved*/) {
log.debug(arguments.callee.name, `Ignoring non-amortization or already approved (i.e. non-editable) JE: ${rec.getValue('tranid')} (ID: ${rec.id})`);
return;
}
// Since we're in the afterSubmit event, we need to load the record in order to persist changes.
rec = record.load({ type: rec.type, id: rec.id });
const LINE_SUBLIST = 'line';
var memo, schedule;
var schedulesByLine = {}
var schedules = [];
// Note: We resort to looping here because the schedulenum field is currently not exposed via SuiteQL
// and the exposed amortizationsched field is null on AJEs.
// Therefore, we cannot directly join all data in one query.
for (var i = 0; i < rec.getLineCount(LINE_SUBLIST); ++i) {
memo = rec.getSublistValue({ sublistId: LINE_SUBLIST, fieldId: 'memo', line: i });
schedule = rec.getSublistValue({ sublistId: LINE_SUBLIST, fieldId: 'schedulenum', line: i });
if (schedule && (memo === 'Amortization Source' || memo === 'Amortization Destination')) {
schedulesByLine[i] = schedule;
if (schedules.indexOf(schedule) < 0) {
schedules.push(schedule);
}
}
}
if (schedules.length > 0) {
log.audit(arguments.callee.name, 'Executing query to retrieve source transactions from schedules: ' + schedules);
var queryResults = query.runSuiteQL({
query:
`SELECT
sch.id,
sch.sourcetran
FROM
AmortizationSchedule AS sch
WHERE
sch.id IN (${schedules.join()})`
}).asMappedResults();
// Goal: For each source transaction, retrieve data from the line tagged with
// the specified amortization schedule (1:1 relationship guaranteed by data model).
var whereClause = '';
queryResults.forEach(result => {
if (whereClause) {
whereClause += ' OR ';
}
whereClause += '(line.transaction = ' + result.sourcetran + ' AND line.amortizationsched = ' + result.id + ')';
});
queryResults = query.runSuiteQL({
query:
`SELECT
trx.tranid,
trx.type,
line.memo,
line.amortizationsched,
line.linesequencenumber,
line.item
FROM
TransactionLine AS line
INNER JOIN Transaction AS trx
ON (trx.id = line.transaction)
WHERE ${whereClause}`
}).asMappedResults();
var lineInfoBySchedule = {};
var lineType;
queryResults.forEach(result => {
lineType = (result.type === 'Journal') ? '' : (result.item ? 'Item ' : 'Expense ');
// Adjust as needed to get the desired info.
// This implementation captures the transaction, line and memo.
lineInfoBySchedule[result.amortizationsched] =
`[${result.tranid} @${lineType}Line ${result.linesequencenumber}] ${result.memo}`;
});
// Now we have all the information, we can update the lines of the AJE
for (i = 0; i < rec.getLineCount(LINE_SUBLIST); ++i) {
schedule = schedulesByLine[i];
if (schedule) {
memo = rec.getSublistValue({ sublistId: LINE_SUBLIST, fieldId: 'memo', line: i });
memo = lineInfoBySchedule[schedule] + ' (' + memo + ')';
rec.setSublistValue({ sublistId: LINE_SUBLIST, fieldId: 'memo', line: i, value: memo });
}
}
rec.save({ ignoreMandatoryFields: true });
} else {
log.debug(arguments.callee.name, 'No schedules found on JE lines');
}
}
return {
afterSubmit: afterSubmit
};
});
The thing is, i'm pretty new to netsuite and i'm not quite sure how to properly enable this script. I have it installed, i setup the settings to the point where i think it should work, but when creating amortization journal entry the memo is still saying Amortization Destination.
Reference to the script: https://netsuite.smash-ict.com/learn-how-to-generate-meaningful-amortization-journal-entry-memo-lines/
Maybe some one has successfully used something like this and could give more detailed explanation on how to set this up? Thanks a lot!
Solution 1:[1]
i'm not quite sure how to properly enable this script. I have it installed, i setup the settings to the point where i think it should work
What settings have you set? To run a User Event script in NetSuite you need three things:
- Create a file containing the script.
- Create a script record referencing the file.
- Create a deployment based on the script record, which tells NetSuite which record(s) to run the script on.
Typically, this can all be done within the flow when you create a new script. Customization > Scripting > Scripts > New, Upload script file, set the required info for the script record, and on the Deployment tab you can select which records to deploy it on.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Krypton |