I need inventory details as csv, there are total of 750k records and I need it in csv, Saved search is not loading in UI and getInputData() is stucked from past 15hrs. How can I do this? multiple csv works.
/** * @NApiVersion 2.1 * @NScriptType MapReduceScript */ define(['N/search', 'N/file', 'N/log'], function(search, file, log) {
const ROW_LIMIT = 100000;
const HEADER = [
'Item ID',
'Price From',
'Wine Owner',
'Document Number',
'Item Rate',
'Price - Rate Diff',
'Status',
'Inventory Number'
];
function getInputData() {
return search.load({
id: 'customsearch_st_itemsaved_search_report_'
});
}
function map(context) {
const result = JSON.parse(context.value);
const row = [
result.values.itemid,
result.values.custitem_pricefrom,
result.values.custitemwine_owner,
result.values["tranid.transaction"],
result.values["rate.transaction"],
result.values.formulanumeric,
result.values["statusref.transaction"],
result.values["inventorynumber.inventoryDetail"]
];
const csvSafe = row.map(v => `"${(v || '').toString().replace(/"/g, '""')}"`);
context.write({
key: 'csv',
value: csvSafe.join(',')
});
}
function reduce(context) {
const chunkSize = ROW_LIMIT;
const header = HEADER.join(',');
let lines = [header];
let part = 1;
context.values.forEach((line, index) => {
lines.push(line);
if ((index + 1) % chunkSize === 0) {
saveCSV(lines, part);
lines = [header];
part++;
}
});
// Save last chunk if needed
if (lines.length > 1) {
saveCSV(lines, part);
}
}
function saveCSV(lines, part) {
const fileName = `ItemSearch_Export_${part}.csv`;
const csvFile = file.create({
name: fileName,
fileType: file.Type.CSV,
contents: lines.join('\n'),
folder: 194669
});
const fileId = csvFile.save();
log.audit(`Saved File ${fileName}`, `File ID: ${fileId}`);
}
return {
getInputData,
map,
reduce
};
});