If the map/reduce script, we can create unique name for the error file by using the time in seconds in the summarize stage.
All the error details are stored as a csv file and will save in a file cabinet with different name for the future reference.
reduceContext.write({
key: ‘error’,
value: { salesOrderInternalID, ‘error’: error }
});
}
const summarize = (summaryContext) => {
try {
let titleArray = [“Record ID”, “Error Description”];
let csvFileData = titleArray.toString() + ‘rn‘;
let recID = ”, fileID = ”, flag = 0;
summaryContext.output.iterator().each(function (key, value) {
let parseSummary = JSON.parse(value);
recID = parseSummary.salesOrderInternalID; // this is the variable name used in pass the error details in the reduce stage which is mentioned above
log.debug(“Record ID in Summary”, recID);
if (key === ‘error’) {
flag = flag + 1
csvFileData += recID + ‘,’ + parseSummary.error.message.replace(‘,’, ” “) + ‘rn‘;
}
return true;
});
if (flag > 0) {
let fileObj = file.create({
name: ‘Name of the File’ + ‘-‘ + Math.floor(Date.now() / 1000) + ‘.csv’,
fileType: file.Type.CSV,
folder: 133294,
contents: csvFileData
});
fileID = fileObj.save();
log.debug(“file obj”, fileID)
email.send({
author: ‘authorID’,
recipients:’ recipientID’,
subject: ‘Enter the subject‘,
body: ‘Email message’,
attachments: [fileObj],
})
}
} catch (e) {
log.error(“error@summarize”, e)
}
}