Created a suit let page were we can enter the source and target account.
The account in the transactions has to be switch from source to target.
If an error occurs the error file will be sent to that respective user.
/**
* @NApiVersion 2.0
* @NScriptType MapReduceScript
* @NModuleScope SameAccount
*/
/**
* Script Description
* This Map Reduce to switch account from sourceAccount to targetAccount.
*
*/
/*******************************************************************************
*
* Support Files
*
* *****************************************************************************
*
* $Author: Jobin & Jismi IT Services LLP $
*
*
******************************************************************************/
define(['N/record', 'N/file', 'N/email', 'N/runtime', 'N/search', 'N/encode'],
function (record, file, email, runtime, search, encode) {
var errorArray = [];
var Obj_content = {}
var fileId;
var main = {
getInputData: function () {
var pinfile = file.create({
name: 'error.txt',
fileType: file.Type.PLAINTEXT,
contents: errorArray.toString(),
folder: 101125,
});
fileId = pinfile.save();
var sourceAccount = JSON.parse(runtime.getCurrentScript().getParameter("custscript_jj_source_account"));
var dataArray = [];
var results = [];
var pageData = search.create({
type: "item",
filters: [
["account", "anyof", sourceAccount]
/* ,
"AND",
["internalid", "anyof", "176"] */
],
columns: [
search.createColumn({
name: "internalid",
label: "Internal ID"
}),
search.createColumn({
name: "type",
label: "Type"
})
]
}).runPaged({
pageSize: 1000
});
var page = pageData.pageRanges;
for (var k = 0; k < page.length; k++) {
var data = pageData.fetch({
index: k
}).data;
for (var j = 0; j < data.length; j++) {
var R = data[j];
var tempObj = {}
tempObj.type = 'item';
tempObj.internalid = R.getValue({
name: "internalid",
label: "Internal ID"
});
tempObj.subtype = R.getValue({
name: "type",
label: "Type"
})
dataArray.push(tempObj)
}
}
var transactionSearchObj = search.create({
type: "transaction",
filters: [
["account", "anyof", sourceAccount]
/* ,
"AND",
["mainline", "is", "T"] */
/* ,
"AND",
["type", "anyof", "Journal"],
"AND",
["internalid", "anyof", "41461"] */
],
columns: [
search.createColumn({
name: "type",
label: "Type"
}),
search.createColumn({
name: "internalid",
label: "Internal ID"
})
]
});
var searchResultCount = transactionSearchObj.runPaged().count;
log.debug("transactionSearchObj result count", searchResultCount);
transactionSearchObj.run().each(function (result) {
var tempObj = {}
tempObj.type = result.getValue({
name: "type",
label: "Type"
});
tempObj.internalid = result.getValue({
name: "internalid",
label: "Internal ID"
});
dataArray.push(tempObj)
return true;
});
return dataArray;
},
reduce: function (context) {
Obj_content = {}
var sourceAccount = JSON.parse(runtime.getCurrentScript().getParameter("custscript_jj_source_account"));
var targetAccount = JSON.parse(runtime.getCurrentScript().getParameter("custscript_jj_target_account"));
var sourceAccountText = runtime.getCurrentScript().getParameter("custscript_jj_source_acc_txt");
var targetAccountText = runtime.getCurrentScript().getParameter("custscript_jj_target_account_txt");
Obj_content.sourceAccount = sourceAccountText;
Obj_content.targetAccount = targetAccountText;
var params = JSON.parse(context.values[0])
try {
if (params.type == "item") {
if (params.subtype == "Description") {
var type = record.Type.DESCRIPTION_ITEM
} else if (params.subtype == "Discount") {
var type = record.Type.DISCOUNT_ITEM
} else if (params.subtype == "Group") {
var type = record.Type.ITEM_GROUP
} else if (params.subtype == "Kit") {
var type = record.Type.KIT_ITEM
} else if (params.subtype == "Markup") {
var type = record.Type.MARKUP_ITEM
} else if (params.subtype == "NonInvtPart") {
var type = record.Type.NON_INVENTORY_ITEM
} else if (params.subtype == "OthCharge") {
var type = record.Type.OTHER_CHARGE_ITEM
} else if (params.subtype == "Payment") {
var type = record.Type.PAYMENT_ITEM
} else if (params.subtype == "Service") {
var type = record.Type.SERVICE_ITEM
} else if (params.subtype == "Subtotal") {
var type = record.Type.SUBTOTAL_ITEM
}
var itemData = record.load({
type: type,
id: params.internalid
});
var tranid = itemData.getValue({
fieldId: 'itemid'
});
Obj_content.type = params.type;
Obj_content.internalid = params.internalid;
Obj_content.tranid = tranid;
var assetAccount = itemData.getValue({
fieldId: 'assetaccount'
});
if (assetAccount == sourceAccount)
itemData.setValue({
fieldId: 'assetaccount',
value: targetAccount
});
var expenseAccount = itemData.getValue({
fieldId: 'expenseaccount'
});
if (expenseAccount == sourceAccount)
itemData.setValue({
fieldId: 'expenseaccount',
value: targetAccount
});
var incomeAccount = itemData.getValue({
fieldId: 'incomeaccount'
});
if (incomeAccount == sourceAccount) {
itemData.setValue({
fieldId: 'incomeaccount',
value: targetAccount
});
}
var liabilityAccount = itemData.getValue({
fieldId: 'liabilityaccount'
});
if (liabilityAccount == sourceAccount)
itemData.setValue({
fieldId: 'liabilityaccount',
value: targetAccount
});
var id = itemData.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug("iditem", id)
} else {
log.debug("else", params)
if (params.type == "VendPymt") {
var featureRecord = record.load({
type: 'vendorpayment',
id: params.internalid
});
var tranid = featureRecord.getValue({
fieldId: 'tranid'
});
Obj_content.type = params.type;
Obj_content.internalid = params.internalid;
Obj_content.tranid = tranid;
//body
var recordAccount = featureRecord.getValue({
fieldId: 'account'
});
if (recordAccount == sourceAccount) {
featureRecord.setValue({
fieldId: 'account',
value: targetAccount
});
}
var id = featureRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug("id", id)
} else if (params.type == "Deposit") {
var featureRecord = record.load({
type: 'deposit',
id: params.internalid
});
var tranid = featureRecord.getValue({
fieldId: 'tranid'
});
Obj_content.type = params.type;
Obj_content.internalid = params.internalid;
Obj_content.tranid = tranid;
//body
var recordAccount = featureRecord.getValue({
fieldId: 'account'
});
if (recordAccount == sourceAccount) {
featureRecord.setValue({
fieldId: 'account',
value: targetAccount
});
}
var linenumber = featureRecord.getLineCount({
sublistId: 'cashback'
});
for (var i = 0; i < linenumber; i++) {
var lineAccount = featureRecord.getSublistValue({
fieldId: 'account',
sublistId: 'cashback',
line: i
});
if (lineAccount == sourceAccount) {
featureRecord.setSublistValue({
sublistId: 'cashback',
fieldId: 'account',
line: i,
value: targetAccount
});
}
}
var linenumber2 = featureRecord.getLineCount({
sublistId: 'other'
});
for (var k = 0; k < linenumber2; k++) {
var lineAccount1 = featureRecord.getSublistValue({
fieldId: 'account',
sublistId: 'other',
line: k
});
if (lineAccount1 == sourceAccount) {
featureRecord.setSublistValue({
sublistId: 'other',
fieldId: 'account',
line: k,
value: targetAccount
});
}
}
var id = featureRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug("id", id)
} else if (params.type == "Check") {
var featureRecord = record.load({
type: 'check',
id: params.internalid
});
var tranid = featureRecord.getValue({
fieldId: 'tranid'
});
Obj_content.type = params.type;
Obj_content.internalid = params.internalid;
Obj_content.tranid = tranid;
//body
var recordAccount = featureRecord.getValue({
fieldId: 'account'
});
if (recordAccount == sourceAccount) {
featureRecord.setValue({
fieldId: 'account',
value: targetAccount
});
}
var linenumber = featureRecord.getLineCount({
sublistId: 'expense'
});
for (var i = 0; i < linenumber; i++) {
var lineAccount = featureRecord.getSublistValue({
fieldId: 'account',
sublistId: 'expense',
line: i
});
if (lineAccount == sourceAccount) {
featureRecord.setSublistValue({
sublistId: 'expense',
fieldId: 'account',
line: i,
value: targetAccount
});
}
}
var id = featureRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug("id", id)
} else if (params.type == "CustPymt") {
var featureRecord = record.load({
type: 'customerpayment',
id: params.internalid
});
var tranid = featureRecord.getValue({
fieldId: 'tranid'
});
Obj_content.type = params.type;
Obj_content.internalid = params.internalid;
Obj_content.tranid = tranid;
//body
var recordAccount = featureRecord.getValue({
fieldId: 'account'
});
if (recordAccount == sourceAccount) {
featureRecord.setValue({
fieldId: 'account',
value: targetAccount
});
}
var id = featureRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug("id", id)
} else {
if (params.type == "Journal") {
var featureRecord = record.load({
type: 'journalentry',
id: params.internalid
});
var tranid = featureRecord.getValue({
fieldId: 'tranid'
});
Obj_content.type = params.type;
Obj_content.internalid = params.internalid;
Obj_content.tranid = tranid;
var linenumber = featureRecord.getLineCount({
sublistId: 'line'
});
for (var i = 0; i < linenumber; i++) {
var lineAccount = featureRecord.getSublistValue({
fieldId: 'account',
sublistId: 'line',
line: i
});
if (lineAccount == sourceAccount) {
featureRecord.setSublistValue({
sublistId: 'line',
fieldId: 'account',
line: i,
value: targetAccount
});
}
}
var id = featureRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug("id", id)
} else if (params.type == "VendBill") {
var featureRecord = record.load({
type: 'vendorbill',
id: params.internalid
});
var tranid = featureRecord.getValue({
fieldId: 'tranid'
});
Obj_content.type = params.type;
Obj_content.internalid = params.internalid;
Obj_content.tranid = tranid;
var linenumber = featureRecord.getLineCount({
sublistId: 'expense'
});
for (var i = 0; i < linenumber; i++) {
var lineAccount = featureRecord.getSublistValue({
fieldId: 'account',
sublistId: 'expense',
line: i
});
var linetaxcode = featureRecord.getSublistValue({
fieldId: 'taxcode',
sublistId: 'expense',
line: i
});
if (lineAccount == sourceAccount) {
featureRecord.setSublistValue({
sublistId: 'expense',
fieldId: 'account',
line: i,
value: targetAccount
});
if (linetaxcode == 5) {
} else {
featureRecord.setSublistValue({
sublistId: 'expense',
fieldId: 'taxcode',
line: i,
value: linetaxcode
});
}
}
}
var id = featureRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
} else if (params.type == "VendCred") {
var featureRecord = record.load({
type: 'vendorcredit',
id: params.internalid
});
var tranid = featureRecord.getValue({
fieldId: 'tranid'
});
Obj_content.type = params.type;
Obj_content.internalid = params.internalid;
Obj_content.tranid = tranid;
var linenumber = featureRecord.getLineCount({
sublistId: 'expense'
});
for (var i = 0; i < linenumber; i++) {
var lineAccount = featureRecord.getSublistValue({
fieldId: 'account',
sublistId: 'expense',
line: i
});
var linetaxcode = featureRecord.getSublistValue({
fieldId: 'taxcode',
sublistId: 'expense',
line: i
});
if (lineAccount == sourceAccount) {
featureRecord.setSublistValue({
sublistId: 'expense',
fieldId: 'account',
line: i,
value: targetAccount
});
if (linetaxcode == 5) {
} else {
featureRecord.setSublistValue({
sublistId: 'expense',
fieldId: 'taxcode',
line: i,
value: linetaxcode
});
}
}
}
var id = featureRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
}
/* }else if(params.type=="Cheque"){
var featureRecord = record.load({
type: 'check',
id: params.internalid
}); */
/* }else if(params.type=="Bill Payment"){
var featureRecord = record.load({
type: 'vendorpayment',
id: params.internalid
}); */
}
}
} catch (er) {
log.debug("er", er.message);
Obj_content.errMSG = er.message.split(".")[0];
var fileLoad = file.load({
id: 119485
});
var fileData = fileLoad.getContents();
if (fileData) {
errorArray = JSON.parse(fileData);
}
errorArray.push(Obj_content);
var pinfile = file.create({
name: 'error.txt',
fileType: file.Type.PLAINTEXT,
contents: JSON.stringify(errorArray),
folder: 101125,
});
pinfile.save();
}
},
summarize: function (summary) {
var fileLoad = file.load({
id: 119485
});
var fileData = fileLoad.getContents();
if (fileData) {
errorArray = JSON.parse(fileData);
}
var emailAddress = runtime.getCurrentScript().getParameter("custscript_jj_email_address");
log.debug("errorArray", errorArray)
if (errorArray[0]) {
if (errorArray.length > 0) {
var myXMLFile = file.load({
id: '119484'
});
var myXMLFile_value = myXMLFile.getContents();
var TABLE = "";
for (var i = 0; i < errorArray.length; i++) {
var Obj_contentget = errorArray[i];
var strVar = "";
strVar += "<Row ss:AutoFitHeight=\"0\">";
strVar += "<Cell><Data ss:Type=\"String\">" + Obj_contentget.type + "<\/Data><\/Cell>";
strVar += "<Cell><Data ss:Type=\"String\">" + Obj_contentget.internalid + "<\/Data><\/Cell>";
strVar += "<Cell><Data ss:Type=\"String\">" + Obj_contentget.tranid + "<\/Data><\/Cell>";
strVar += "<Cell><Data ss:Type=\"String\">" + Obj_contentget.sourceAccount + "<\/Data><\/Cell>";
strVar += "<Cell><Data ss:Type=\"String\">" + Obj_contentget.targetAccount + "<\/Data><\/Cell>";
strVar += "<Cell><Data ss:Type=\"String\">" + Obj_contentget.errMSG + "<\/Data><\/Cell>";
strVar += "<\/Row>";
if (i < (errorArray.length - 1)) {
strVar = strVar + '\n';
}
TABLE = TABLE + strVar;
}
myXMLFile_value = myXMLFile_value.replace('<!-- REPLACEWITHTABLEBODY -->', TABLE);
myXMLFile_value = myXMLFile_value.replace(/;/g, "|");
myXMLFile_value = myXMLFile_value.replace(/&/g, '&');
var strXmlEncoded = encode.convert({
string: myXMLFile_value,
inputEncoding: encode.Encoding.UTF_8,
outputEncoding: encode.Encoding.BASE_64
});
var fileObj = file.create({
name: 'Errorfile',
fileType: file.Type.EXCEL,
contents: strXmlEncoded
});
fileObj.folder = 101125;
var fileId = fileObj.save();
email.send({
author: -5,
recipients: emailAddress,
subject: 'ERROR IN SWITCHING ACCOUNTS',
body: 'ERROR RECORDS AND ITS DETAILS',
attachments: [fileObj]
});
}
} else {
email.send({
author: -5,
recipients: emailAddress,
subject: 'Switching Accounts',
body: 'Switching of the account from source to target is completed.'
});
}
},
};
for (var key in main) {
if (typeof main[key] === 'function') {
main[key] = trycatch(main[key], key);
}
}
function trycatch(myfunction, key) {
return function () {
try {
return myfunction.apply(this, arguments);
} catch (e) {
log.debug("e in " + key, e);
}
}
};
return main;
});