OL Learn

Update a file that already exists from datamapper Postprocessor

Hi,

I need to generate daily reports of invalid invoices. I am using a postprocessor script which outputs a csv file with the list of such invoice numbers in the incoming PDF files.
This works great on the first run of the day, but the datamapper fails in subsequent runs because the report file name already exists on disk.

The error message is as follow:

Error running postprocessors ( Wrapped java.nio.file.FileAlreadyExistsException: C:\Production\Invoices\Reports_20210211.txt (update_daily_report#10))

How can I open the existing report file to append data new lines to it and save when the datamapping steps completes.

My script is as follow;

var ErrorFilePath = automation.parameters.ErrorFilePath
var fileOut = openTextWriter(ErrorFilePath );
var counter = 0;

for (var i = 0; i < TotalRecords ; i++){
var str = [];

if(data.records[i].fields.isValid == false){	
	counter++;	
	if(counter == 1){
		var description = '**** LIST OF INVOICES WHICH COULD NOT BE VALIDATED IN THE DATABASE *****';
		fileOut.write(description);
		fileOut.newLine();
		var headers = 'CustomerID;InvID;CustomerName;InvTotal;InvDate;InvPayByDate;Reason';
		fileOut.write(headers);
		fileOut.newLine();
	}
	str.push(data.records[i].fields.CustomerID);
	str.push(data.records[i].fields.InvID);
	str.push(data.records[i].fields.CustomerName);
	str.push(data.records[i].fields.InvTotal);
	str.push(data.records[i].fields.InvDate);
	str.push(data.records[i].fields.InvPayByDate);
	str.push(data.records[i].fields.InvalidReason);
	fileOut.write(str.join(';'));
	fileOut.newLine();
}  

}
fileOut.close();

See this online help page for the parameters you can pass to openTextWriter().