Iterate through records in postprocessor

I have a set of records in DataMapper and using the postprocessor I want to iterate through each record and if the record has a column that matches an account number, export the data to a CSV file. It everything works except it only exports the data for the record that is currently selected, it doesn’t iterate through all records.

Here is the code I am using. data.records.length is always 1, even though there are 4 records in the data set. How can I get this to go through each record?

for (var i=0; i < data.records.length;i++){
if(data.records[i].fields[“Account Number”]== “3799”){

//write rows to CSV file

}

For all I know, the targeted account number could the last record in your data file.

I think you should first iterate through the records and check whether this account number exists. If it does at any iteration in the loop, set a Boolean field checking for the existence of this account to true and immediately exit the loop

Then loop again, but this time to write the csv knowing your targeted account exists

var accountNumberExists = false;
var recordLength = data.records.length;
for (var i=0; i < recordLength;i++){
	if(data.records[i].fields["Account Number"]== "3799"){
 		accountNumberExists = true;
 		break;
	}
}

if(accountNumberExists){
	for(var i=0; i < recordLength; i++){
	//write rows to CSV file
	}
}

That is exactly what I am doing. But records.length is always just 1 so the loop never gets past the record that is currently selected in datamapper.

Try running your data mapping config through the Execute Data Mapping plugin in Workflow.

At design time, only the current record is “seen” by the Post-Processor since none of the other records have been stored in the database yet. So running the same procedure from Workflow will work as expected because all records will have been stored in the DB before the post-Processor is executed.