I feel this is a simple fix but I have been going around in circles and do not want to use up all our Flow credits. I am trying to create a CSV file using a Postman Flow. It is passing the data through but not creating a new line for each CSV line created.
function convertToCSV(objArray: any[], columns: string[]): string {
const array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
let str = '';
let row = '';
str = "VIN,Date,Memo,Amount,Type\r\n";
for (let i = 0; i < array.length; i++) {
let line = '';
for (let index of columns) {
if (index === 'custcol1') {
truckVIN = array[i][index];
}
if (line !== '') line += ',';
line += array[i][index] || '';
}
str += line + '\r\n';
}
I couldn’t get the function you wrote to execute in the Evaluate Block as is, but I re-wrote it slightly and this seemed to work:
function convertToCSV(objArray: any[], columns: string[]): string {
const array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray;
let csvString = 'VIN,Date,Memo,Amount,Type\r\n'; // Adding the header row
for (let i = 0; i < array.length; i++) {
let line = '';
for (let j = 0; j < columns.length; j++) {
if (j > 0) line += ','; // Adding a comma before each column except the first one
const value = array[i][columns[j]] || ''; // Fallback to empty string if value is undefined
line += value;
}
csvString += line + '\r\n'; // Adding a newline at the end of each row
}
return csvString;
}