For Next Loop in JavaScript Field failing

Hello All

I have simplified the example data for illustration.

I am trying to process a file that has field names F-1 to F-15. I want to populate a JavaScript field with the contents of one of the fields F-1 to F-15 if it contains the letter B. The first “B” to be found should end the loop.

I can create a script with multiple “if then else” statements but feel a loop would be simpler but sadly I get an Error running script (Undefined script result (DME000018)) (DME000019)

My script is as follows, if anyone has any pointers please do share. My JavaScript skills are still being developed, maybe you can tell?

for(let i=1;i>=15;i++) {
if (record.fields[“F-” + i].includes(“B”))
{ record.fields[“F-” + i]
break
} else { “” }}

Many Thanks

You say the data file has field names F-1 to F-15, but you don’t specify what kind of data file it is.

For a CSV or a Database, you’d use something like:

var result = "";
for(var i=1; i<=15; i++){
  var dataField = data.extract("F-"+i,0);
  if(dataField[0]=="B"){
    result = dataField;
    break;
  }
}
result;

For XML, something like this:

var result = "";
for(var i=1; i<=15; i++){
var dataField = data.extract('./F-'+i,0);
  if(dataField[0]=="B"){
    result = dataField;
    break;
  }
}
result;

Many Thanks for the swift response Philippe.

Sorry I never mentioned it was a CSV file.

It worked like a charm.