Record.copy is not working
I am trying to copy the BOM and create new BOM and update the Name of the BOM with new data but for some reason record.copy is not returning any value, could you please help.
/**
* @NApiVersion 2.x
* @NScriptType MapReduceScript
*/
define([‘N/log’, ‘N/record’, ‘N/file’],
function(log, record, file) {
function getInputData() {
// Specify the internal ID of the CSV file in the File Cabinet
var fileId = ‘2196737’;
// Read CSV file content
var csvFile = file.load({
id: fileId
});
var csvData = csvFile.getContents();
var lines = csvData.split(/\r\n|\n/);
var headers = lines[0].split(“,”);
// Prepare data for creating new BOMs
var bomData = [];
for (var i = 1; i < lines.length; i++) {
var currentLine = lines[i].split(“,”);
var oldBom = currentLine[headers.indexOf(“oldBom”)];
var newBom = currentLine[headers.indexOf(“newBom”)];
bomData.push({
oldBOMId: oldBom,
newBOMData: newBom
});
}
return bomData;
}
function map(context) {
var entry = JSON.parse(context.value);
var oldBOMId = entry.oldBOMId;
var newBOMData = entry.newBOMData;
// Log old BOM ID and new BOM data for debugging
log.debug(‘Old BOM ID’, oldBOMId);
log.debug(‘New BOM Data’, newBOMData);
try {
// Copy old BOM
var newBOMId = record.copy({
type: record.Type.BOM,
id: oldBOMId,
isDynamic: true
});
log.debug(‘New BOM ID’, newBOMId);
// Emit new BOM ID as the key and new BOM data as the value
context.write({
key: newBOMId,
value: newBOMData
});
} catch (ex) {
// Log any errors that occur during BOM creation/modification
log.error({
title: ‘Error Creating/Modifying BOM’,
details: ex
});
}
}
function reduce(context) {
// Nothing to do in reduce stage for this scenario
}
function summarize(summary) {
// Log script execution results
log.audit(‘Script Execution Summary’, ‘Map/Reduce Script has been executed.’);
log.audit(‘Number of Records Processed’, summary.inputSummary.processed);
log.audit(‘Number of Records Skipped’, summary.inputSummary.skipped);
log.audit(‘Number of Records Failed’, summary.inputSummary.failed);
}
return {
getInputData: getInputData,
map: map,
reduce: reduce,
summarize: summarize
};
});
You never call save() after the copy. Also, somebody answered your question on Reddit yesterday.
I understand its not necessary to call the save when we use record.copy but I tried that too but its not working and its says “A Bill of Materials with this name already exists, please use another”
So set a new name before saving.



