/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
* @NModuleScope SameAccount
*/
define([ 'N/record', 'N/search'],
(record,search) => {
getInputData = (context) => {
log.debug('==START==','==START==');
// first value is internal id of customer deposit, second value is amount to create journal for
let customerDepositData = [
["22447","100.00"]
];
return customerDepositData;
}
map = (context) => {
log.debug('context', JSON.stringify(context));
let data = JSON.parse(context.value);
try {
let customerDepositData = getCustomerDepositData(data[0]);
let jeId = createJournalEntry(customerDepositData, parseFloat(data[1]));
let depositApplicationRecordId = createDepositApplication(jeId, customerDepositData);
if (!depositApplicationRecordId) {
// failed to make deposit application so delete the journal entry
record.delete({
type: record.Type.JOURNAL_ENTRY,
id: jeId
});
log.error('Journal was deleted as deposit application failed for ' + data[0]);
}
}
catch(e) {
log.error('Error when processing customer deposit ' + data[0], JSON.stringify(e));
}
}
getCustomerDepositData = (internalId) => {
let searchObj = search.create({
type: "customerdeposit",
filters: [
["internalid", "is", internalId],
"AND",
["mainline", "is", "T"]
],
columns: [
"entity"
]
});
let data = {};
searchObj.run().each(function (result) {
data.id = result.id;
data.entity = result.getValue('entity');
return true;
});
return data;
}
createDepositApplication = (jeId, customerDepositData) => {
try {
let depositApplicationRecord = record.transform({
fromType: record.Type.CUSTOMER_DEPOSIT,
fromId: customerDepositData.id,
toType: record.Type.DEPOSIT_APPLICATION,
isDynamic: true
});
let lineCount = depositApplicationRecord.getLineCount({sublistId: 'apply'});
for (let i = 0; i < lineCount; i++) {
let currentLine = depositApplicationRecord.selectLine({sublistId: 'apply',line: i});
let testId = depositApplicationRecord.getCurrentSublistValue({sublistId: 'apply',fieldId: 'doc'});
if (testId == jeId) {
depositApplicationRecord.setCurrentSublistValue({sublistId: 'apply',fieldId: 'apply',value: true});
depositApplicationRecord.commitLine({sublistId: 'apply'});
break;
}
}
let depositApplicationRecordId = depositApplicationRecord.save();
log.debug('Created deposit application with Id', depositApplicationRecordId);
return depositApplicationRecordId;
}
catch(e) {
log.error('Failed to create deposit application', JSON.stringify(e));
}
return false;
}
createJournalEntry = (customerDepositData, amount) => {
let accounts = {
'ar_account' : 6,
'bad_debt_account' : 63
};
let jeRecord = record.create({
type: 'journalentry',
isDynamic: true
});
jeRecord.setValue('subsidiary', 1);
// add debit
let lineNum = jeRecord.selectNewLine({sublistId: 'line'});
jeRecord.setCurrentSublistValue({sublistId: 'line',fieldId: 'account',value: accounts.ar_account});
jeRecord.setCurrentSublistValue({sublistId: 'line',fieldId: 'debit',value: amount});
jeRecord.setCurrentSublistValue({sublistId: 'line',fieldId: 'entity',value: customerDepositData.entity});
jeRecord.commitLine({sublistId: 'line'});
// add credit
let lineNum = jeRecord.selectNewLine({sublistId: 'line'});
jeRecord.setCurrentSublistValue({sublistId: 'line',fieldId: 'account',value: accounts.bad_debt_account});
jeRecord.setCurrentSublistValue({sublistId: 'line',fieldId: 'credit',value: amount});
jeRecord.setCurrentSublistValue({sublistId: 'line',fieldId: 'entity',value: customerDepositData.entity});
jeRecord.commitLine({sublistId: 'line'});
let recordId = jeRecord.save();
log.debug('Created journal with Id', recordId);
return recordId;
}
summarize = (summary) => {
log.debug('==END==','==END==');
}
return {
getInputData: getInputData,
map: map,
summarize: summarize
};
});
Jaime Requena is a seasoned NetSuite Consultant and Solutions Architect, known for delivering WHITE GLOVE service to businesses. With 15+ years of experience and 3x certifications in ERP, Developer, and Admin, Jaime specializes in highly customized NetSuite accounts, transforming operations for 200+ satisfied customers all across the globe.
We aim to bring unmatched expertise and professionalism to your NetSuite initiatives. Let’s talk about how our NetSuite consultancy can make a difference!
It’s Been 4+ Years Now And We Have Worked With Hundreds Of Clients, Building Our Way To The Top, One Happy Client After Another! Their Voices Of Satisfaction Serve As A Testament To Our Success –