A file contains the errors that are created on the same date
function fileCreate(document, errorMessage, errorDate) {
try {
let dateArray = [];
let csvData = “Order ID, Error message\r\n”;
csvData += document + ‘,’ + errorMessage + “\r\n”;
let folderSearchObj = search.create({
type: “folder”,
filters:
[
[“internalid”, “anyof”, ” “]
],
columns:
[
search.createColumn({ name: “numfiles”, label: “# of Files” }),
search.createColumn({
name: “name”,
join: “file”,
label: “Name”
}),
search.createColumn({
name: “internalid”,
join: “file”,
label: “Internal ID”
}),
search.createColumn({
name: “created”,
join: “file”,
label: “Date Created”
})
]
});
var searchResultCount = folderSearchObj.runPaged().count;
log.debug(“folderSearchObj result count”, searchResultCount);
folderSearchObj.run().each(function (result) {
let numberOfFiles = result.getValue({ name: “numfiles” })
let fileInternalId = result.getValue({ name: “internalid”, join: “file” });
let fileName = result.getValue({ name: “name”, join: “file” });
let dateOfFileCreate = result.getValue({ name: “created”, join: “file” });
let formattedDate = dateOfFileCreate.split(‘ ‘)[0].split(‘/’).reverse().join(‘-‘);
if ((numberOfFiles == 0) || ((numberOfFiles > 0) && (!dateArray.includes(errorDate)))) {
let fileObj = file.create({
name: ‘Transfer Order Creation Errors ‘ + (new Date()).toLocaleDateString(‘en-GB’).replace(/\//g, ‘-‘) + ‘.csv’,
fileType: file.Type.CSV,
contents: csvData,
folder: // replace with the ID of the folder where you want to save the file
});
dateArray.push((new Date()).toLocaleDateString(‘en-GB’).replace(/\//g, ‘-‘));
let testvalue = fileObj.save();
} else if ((numberOfFiles > 0) && (formattedDate == errorDate)) {// if file is present load that file
let fileObj = file.load({
id: fileInternalId
});
let newContent = document + ‘,’ + errorMessage + “\r\n”;;
// Get the existing file content
let existingContent = fileObj.getContents();
// Append the new content
let appendedContent = existingContent + newContent;
//We cannot update the file content ,so we need to create that file with same name
let fileNew = file.create({
name: fileObj.name,
fileType: fileObj.fileType, // change it depending the file type you will create
contents: appendedContent,
folder: fileObj.folder
});
// Save the changes
let fileId = fileNew.save();
}return true;
});
}catch(e){
log.debug(“error”,e)
}
}