Creating a csv output from xml in the Postprocessor

Hi - I am trying to get some help with a script I am trying to do in the postprocessor. I am trying to create a csv out put from my xml datafile. The first loop I am using works fine. My issue is in creating my second loop because I have a detail table I am trying to get at and I am not having any luck. Can anyone help me with the second loop? No matter what I try - I always end up with an undefined of something type error. I was using ‘twoRec’ for my second loop attempt. This is my last resort for help as I have had someone alot more experienced in javascript than I am trying to help me but he can’t figure it out either. I did ask this question back on July 24 and the response I received did not work so after working for three weeks on this below is where I am at. Any help would be greatly appreciated. - Thank you in advance

var fileOut = openTextWriter(“c:\out\BankSummaryFee.csv”);
var str=, oneRec, twoRec, prop;
// Add Column names as first line in CSV
oneRec = data.records[0];
twoRec = data.records.tables[APVendor];
for (prop in oneRec.fields){
str.push(‘"’+prop+‘"’);
for (prop in twoRec.fields)
str.push(‘"’+prop+‘"’);
}
fileOut.write(str.join(“,”));
fileOut.newLine(); str = ;
// Add all values for all records
for (var i=0; i < data.records.length;i++){
for (var s=0; s < data.records.tables[APVendor].length;i++){
for (prop in oneRec.fields) {
str.push(‘"’+oneRec.fields[prop]+‘"’);
for (prop in twoRec.fields) {
str.push(‘"’+twoRec.fields[prop]+‘"’);
}}
fileOut.write(str.join(“,”));
fileOut.newLine();
str = ;
}
//Close file
fileOut.close();

Thank you

The value you assign totwoRecis incorrect.

It should be either:

	twoRec = oneRec.tables["APVendor"]; // notice the quotes

or

	twoRec = oneRec.tables.APVendor;

I haven’t tested it here but the entire code should look something like this:

var fileOut = openTextWriter("c:\\out\\BankSummaryFee.csv");
var str = [], oneRec, twoRec, prop;

// Add Column names as first line in CSV
oneRec = data.records[0];
twoRec = oneRec.tables["APVendor"];
for (prop in oneRec.fields) {
   str.push('"' + prop + '"');
}
for (prop in twoRec.fields) {
   str.push('"' + prop + '"');
}

fileOut.write(str.join(","));
fileOut.newLine();

str = [];
// Add all values for all records
for (var i = 0; i < data.records.length; i++) {
   oneRec = data.records[i];
   twoRec = oneRec.tables["APVendor"];
   for (prop in oneRec.fields) {
      str.push('"' + oneRec.fields[prop] + '"');
   }
   for (prop in twoRec.fields) {
      str.push('"' + twoRec.fields[prop] + '"');
   }
   fileOut.write(str.join(","));
   fileOut.newLine();
   str = [];
}
//Close file
fileOut.close();

There may be typo here and there (like I said, I haven’t tested it), but you should get the gist of it.

Let me know if that helps.

Hi Phil - thank you. I have tried the code above and while I do not get any errors, the only data it is producing is from the first loop. For example - my data structure looks like this.

record [1]
ExtraData
RunDate 06/21/2017
Payable 06/30/2017

APVendor [3]
ExtraData
VendorId ADIFIN7868
Name Adirondack Financial Services
PayType
TotalRaised 275.92

and so on… My output looks like this

“RunDate”,“Payable”,“ExtraData”
“06/21/2017”,“06/30/2017”,“”

Sorry about that, as I said I hadn’t tested it here… and I should have!

Here’s the proper code:

var fileOut = openTextWriter("c:\\out\\BankSummaryFee.csv");
var str = [], oneRec, twoRec, prop;

// Add Column names as first line in CSV
oneRec = data.records[0];
twoRec = oneRec.tables["APVendor"];
for (prop in oneRec.fields) {
   str.push('"' + prop + '"');
}
for (prop in twoRec[0].fields) {
   str.push('"' + prop + '"');
}

fileOut.write(str.join(","));
fileOut.newLine();

str = [];
// Add all values for all records
for (var i = 0; i < data.records.length; i++) {
   oneRec = data.records[i];
   twoRec = oneRec.tables["APVendor"];
   for (var j=0;j<twoRec.length;j++){
      for (prop in oneRec.fields) {
         str.push('"' + oneRec.fields[prop] + '"');
      }
      for (prop in twoRec[j].fields) {
         str.push('"' + twoRec[j].fields[prop] + '"');
      }
      fileOut.write(str.join(","));
      fileOut.newLine();
      str = [];
   }
}
//Close file
fileOut.close();

No problem at all - I’m glad someone is helping.

I ran the code you provided and it works great. Thank you so much for your help!!!

I corrected my post just seconds after posting it, I had left my own table name in there. Did you try the version that is currently posted because it works fine on my end.

All right, all seems in sync now. Glad I could help.