Detail Table Field - Convert to Script

I need to add logic to a field in a detail table. When I convert to a script, I am getting a loop error. I am not sure how to fix it?

What I am trying to do is when the first row in the detail table is equal to Personal Property, I need to use a different address field in the data (“field2”). All other rows can use the “field” address. There is a maximum of 12 rows

var field = record.tables.Properties[‘TPAddress1’];
var field2 = record.fields[‘DelAddr’];
var field3 = record.tables.Properties[‘Tax1Description’];

for (var i = 1; i < 12; i++) {
if ((i=1) && (field3 = ‘Personal Property’)) {
this.text(field2);
}else{
this.text(field);
}
}

Hi @APoole,

That should be “i == 1”. With “i = 1” you don’t compare i to 1 but you assign the value 1 to i, so the loop never ends.

Likewise: “field3 == ‘Personal Property’”.

Perfect! That get’s rid of the loop error, but now the results are all coming back as undefined in my preview?

Oops, just realized you are using “each matched element” as a scope, scratch that.

Well I fixed the undefined, but the first record doesn’t show a different address…

var field = this.record.fields[‘TPAddress1’];
var field2 = record.fields[‘DelAddr’];
var field3 = this.record.fields[‘Tax1Description’];

for (var i = 1; i <= 12; i++) {
if ((i == 1) && (field3 == ‘Personal Property’)) {
this.text(field2);
}else{
this.text(field);
}
}

var field = this.record.fields['TPAddress1'];
var field2 = record.fields['DelAddr'];
var field3 = this.record.fields['Tax1Description'];

if (this.index == 0 && field3 == 'Personal Property') {
    this.text(field2);
} else {
    this.text(field);
}

Something like that? Note that the first record is index 0, not 1.

When you use “each matched element” as a scope there is a hidden loop around the script, so you don’t need that for-loop.

1 Like

YES!!! Thank you! This is exactly what I needed.