How to build Nested Loops - Help!

How to build Nested Loops - Help!
0

#1

Hi - I am fairly new to Javascript and trying to use a nested for loop. My data source is an xml with nested tables and I am trying to create a csv output with data from one of the nested tables called ‘APVendor’. I have the first loop working but can’t seem to get the second loop to work. No matter what I try I get ‘undefined’ errors. My first for loop looks like this -

var fileOut = openTextWriter("c:\\out\\BankSummaryFee.csv");
var str=[], oneRec, prop;
// Add Column names as first line in CSV
oneRec = data.records[0];
for (prop in oneRec.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 (prop in oneRec.fields) {
str.push('"'+oneRec.fields[prop]+'"');
}
fileOut.write(str.join(","));
fileOut.newLine();
str = [];
}
//Close file
fileOut.close();

I have tried 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 = 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();

I know this isn’t right but have tried several different ways. Can anyone help me?


#2

You might want to ask this question on Objectif Lune’s forum, because openTextWriter is a function that can be used within the PlanetPress Suite API.


#3

This is actually a post processor script in Connect PP. I went to the forum first a couple times. Still can’t get it to work.

I’ll keep trying. Thank you!!


#4

Do you have a link to the documentation? Also, what is the link to the forum there?


#5

I have a .pdf that was shared with me to get thru the first loop. Which works great. The link to my communication on the forum is Postprocessor - creating csv from xml.

The documentation I have to get the first loop is in pdf format. I am not sure how or if I can attach a pdf. The script and a ‘note’ from that pdf is below.

var fileOut = openTextWriter("c:\\test\\Clients.csv");
var str=[], oneRec, prop;
// Add Column names as first line in CSV
oneRec = data.records[0];
for (prop in oneRec.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 (prop in oneRec.fields) {
str.push('"'+oneRec.fields[prop]+'"');
}
fileOut.write(str.join(","));
fileOut.newLine();
str = [];
}
//Close file
fileOut.close();

“Note how generic this code is: it does not refer to any of the fields by name. The code uses javascript reflection to inspect the fields array and extract the names and values for each field. Using the same method, you could write additional code that exports all detail tables in all records without having to know what field names are
used in the data model.”


#6

Just wanted to say that this issue has been resolved. Thank You.


#7

So, what was the solution?


#8

I wasn’t coding 'twoRec for the second loop correctly. I needed to say this -
oneRec = data.records[0];
twoRec = oneRec.tables[“APVendor”];

I was trying to use this -
oneRec = data.records[0];
twoRec = data.records.tables[APVendor];