ashokkumar9640444
09/25/2025, 4:58 PM|
) delimiter. The script fetches details from a saved search, prepares the file, and saves it in a folder.
The saved search contains around 50,000 results. After creating the file and comparing it with the saved search export, the data does not match (amounts and lines are getting jumbled).
However, when I tested with a smaller dataset of about 2,000 results, the created file matched the saved search export correctly.
Here is the code I used. Can anyone please let me know why the data is not fetching/matching correctly with the saved search in the code below?
Has anyone faced this type of issue.
// Latest Back Up Code of Board File - 09/15/2025
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(['N/search', 'N/file', 'N/log'], (search, file, log) => {
const SAVED_SEARCH_ID = '15833';
const OUTPUT_FILE_NAME = 'Transactions_Aug_2025_pipe_delimited_1.txt';
const OUTPUT_FOLDER_ID = 1178218;
const getInputData = () => {
const savedSearch = search.load({ id: SAVED_SEARCH_ID });
const pagedData = savedSearch.runPaged({ pageSize: 1000 }); // 1000
return pagedData.pageRanges;
};
const map = (context) => {
const pageRange = JSON.parse(context.value);
const savedSearch = search.load({ id: SAVED_SEARCH_ID });
const pagedData = savedSearch.runPaged({ pageSize: 1000 });
const page = pagedData.fetch({ index: pageRange.index });
page.data.forEach((result) => {
const row = result.columns.map((col) => {
let value = result.getText(col) || result.getValue(col) || '';
value = value.toString()
.replace(/\r?\n|\r/g, ' ')
.replace(/\|/g, '\\|');
return value;
}).join('|');
context.write({ key: 'results', value: row });
});
};
const reduce = (context) => {
const partialContent = context.values.join('\n');
context.write({ key: 'output', value: partialContent });
};
const summarize = (summary) => {
const savedSearch = search.load({ id: SAVED_SEARCH_ID });
const columns = savedSearch.columns;
const header = columns.map((col) => {
let label = col.label || col.name;
label = label.replace(/\|/g, '\\|');
return label;
}).join('|');
let fullContent = header + '\n';
summary.output.iterator().each((key, value) => {
fullContent += value + '\n';
return true;
});
const fileObj = file.create({
name: OUTPUT_FILE_NAME,
fileType: file.Type.PLAINTEXT,
contents: fullContent,
folder: OUTPUT_FOLDER_ID
});
const fileId = fileObj.save();
log.audit('File Created', Pipe-delimited file created with ID: ${fileId});
if (summary.inputSummary.error) {
log.error('Input Error', summary.inputSummary.error);
}
summary.mapSummary.errors.iterator().each((key, error) => {
log.error(Map Error for key: ${key}, error);
return true;
});
summary.reduceSummary.errors.iterator().each((key, error) => {
log.error(Reduce Error for key: ${key}, error);
return true;
});
};
return { getInputData, map, reduce, summarize };
});
Anthony OConnor
09/25/2025, 5:16 PMAnthony OConnor
09/25/2025, 5:16 PMAnthony OConnor
09/25/2025, 5:18 PMMichael Pope
09/25/2025, 6:06 PMMichael Pope
09/25/2025, 6:06 PMalien4u
09/25/2025, 6:07 PM//Add additional code ...
var searchTask = task.create({
taskType: task.TaskType.SEARCH
});
searchTask.savedSearchId = 51;
var path = 'ExportFolder/export.csv';
searchTask.filePath = path;
var searchTaskId = searchTask.submit();
ashokkumar9640444
09/26/2025, 6:23 PMtask.create
, how can I split the data into multiple files?Anthony OConnor
09/26/2025, 6:32 PMAnthony OConnor
09/26/2025, 6:32 PMAnthony OConnor
09/26/2025, 6:39 PM// create and name the empty file first
const fileObj = file.create({
name: filename,
fileType: file.Type.CSV,
folder: targetFolderId,
});
const fileId = fileObj.save();
//pass in the fileId to the search task
const searchTask = task.create({
taskType: task.TaskType.SEARCH,
savedSearchId: savedSearchId,
fileId: fileId,
});
// create your script task
const scheduledScriptTask = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT
});
// set the script options on the task
scheduledScriptTask.scriptId = dependentScript;
scheduledScriptTask.deploymentId = dependentDeploy;
scheduledScriptTask.params = { [dependentParam] : fileId };
// add the dependency to the search task
searchTask.addInboundDependency(scheduledScriptTask);
//submit search task
const taskId = searchTask.submit();
log.debug("task id", taskId);
Anthony OConnor
09/26/2025, 6:41 PMAnthony OConnor
09/26/2025, 6:41 PMashokkumar9640444
09/29/2025, 4:30 PM