Script writes fields to CSV out of order

I have been using a Postprocessor script to write fields extracted from PDF data sources to CSV files for half a year now, but I have run into a strange issue with the latest DataMapper. The fields in the CSV file are not written in the same order they appear in the DataMapper.

Here is the script

var fileOut = openTextWriter("D:\\Shared Folders\\Working Ignite\\2. Utilities\\Oakdale\\oakdaleaddressdata.csv","UTF-8");
var str=[],headers=[],oneRec;

// Add all values for all records
for (var i=0;i<data.records.length;i++){
    oneRec = data.records[i];
    for (var prop in oneRec.fields) {
        // Add field names as headers
        if(headers){
            headers.push('"'+prop+'"');
        } 
        str.push('"'+oneRec.fields[prop]+'"');
    }
    if(headers) {
        fileOut.write(headers.join(","));
        fileOut.newLine();
        headers=null;
    }
    fileOut.write(str.join(","));
    fileOut.newLine();
    str = [];
}

//Close file
fileOut.close();

Any ideas why this is happeneing?

If it is reproducible at will, your best bet is to open a technical support ticket through our website.

An agent will then confirm it reproducible and escalate it to R&D.

@rdaneel72, I’m not sure if you’ve already opened a ticket for this but you may want to check what the result of the script is by logging the lines written to the CSV file in the Data Mapper engine logs. This can be achieved by making the following adjustments, for example.

var lines = [];
for (var i = 0; i < data.records.length; i++) { /* ... */ }
fileOut.write(headers.join(","));
lines.push(headers.join(","));
fileOut.write(str.join(","));
lines.push(str.join(","));
fileOut.close();
logger.info("Result: " + JSON.stringify(lines));

To check the result you would need to:

  1. Run a print from the Designer (Ctrl + P)
  2. Check the DataMapper engine logs stored under
    C:\Users\{User}\Connect\logs\Datamapperengine
  3. Search for “Result:” (without the quotes)

Thank you for the responses, but other factors have resulted in me going in a different direction on this project, so this is no longer an issue.

I do appreciate the helpful and welcoming discourse on this forum for trying to assist a dummy like me.

1 Like