Tap Forms JavaScript Scripting 102
Forms, Records, Fields, Values
by T. L. Ford

Section 3: Looping through Records

We now understand that variables can represent objects. If we know what kind of object that variable is, we know what methods and properties it has (from the Tap Forms JavaScript API documentation).

Said another way: If we know what data we are looking for, we can find the right method in the Tap Forms JavaScript API documentation and figure out what objects we need in order to get to that data.

Suppose we want to list the names of the animals we've rescued.

We know we want the records on the 'Rescue Animals' form and we know we want the value from the 'Animal Name' field of each record.

We start with getting the form object.

var rescue_animals_form = document.getFormNamed('Rescue Animals');

We're going to need the records from that form, so the form's getRecords() is our form method. It returns an array of record objects.

var rescue_animals_records = rescue_animals_form.getRecords();

We know that getRecords() returns an array of record objects, and we know that if we want to do something to elements in the array, we use a loop.

We can insert the Basic loop snippet,

for (var index = 0, count = records.length; index < count; index++){
  // do something;
}

and then replace the array variable name to ours.

for (var index = 0, count = rescue_animals_records.length; index < count; index++){
  // do something;
}

Next we need to figure out how to get the 'Animal Name' field value.

We know that rescue_animals_records[index] will give us the record and we can use record.getFieldValue(id) to get the value. Then we can print to the console.

for (var index = 0, count = rescue_animals_records.length; index < count; index++){
  var value = rescue_animals_records[index].getFieldValue(field_id);
  console.log(value);
}

We need the field's ID. We get that from the form's field. We get the form's field from the form.

var rescue_animals_name_field = rescue_animals_form.getFieldNamed('Animal Name');
var rescue_animals_name_id = rescue_animals_name_field.getId();

We don't need to redo that every loop because the variable isn't going to change each loop. We put it before the loop, making our code look like:

function List_All_Animal_Names() {
 
  var rescue_animals_form = document.getFormNamed('Rescue Animals');
 
  var rescue_animals_name_field = rescue_animals_form.getFieldNamed('Animal Name');
  var rescue_animals_name_id = rescue_animals_name_field.getId();
 
  var rescue_animals_records = rescue_animals_form.getRecords();
 
  for (var index = 0, count = rescue_animals_records.length; index < count; index++){
    var value = rescue_animals_records[index].getFieldValue(field_id);
    console.log(value);
  }
 
}
 
List_All_Animal_Names();

We run it and...

Whoops. That red text in the console is an error!

When you get a red error message (and you will!), read it backwards. "line 11" (the error is likely on this line OR a line before it). I generally ignore the column, other than to note if the error is early (< 15) or later (> 14) in the line. "Can't find variable field_id" - Well! Why not?! I go back to my code, looking at the indicated line.

You may notice I skipped the first part of that error message, "List All Animal Names: ReferenceError:". That's the name of the function and some useful text to plug into a search engine if the error makes no sense. If I were searching for help on the Internet, I would put in: "Tap Forms ReferenceError: Can't find variable". The name of my function and variable aren't needed and will confuse the search engine.

Woohoo! I'm not alone with this error message. If the compiler can't find the variable, I didn't define it before this point in the code. I can read through my code and see that I didn't define "field_id" so either I need to define it OR I need to figure out what the right variable is. Ah hah! I forgot to rename the field_id placeholder text we put in to rescue_animals_name_id.

function List_All_Animal_Names() {
 
  var rescue_animals_form = document.getFormNamed('Rescue Animals');
 
  var rescue_animals_name_field = rescue_animals_form.getFieldNamed('Animal Name');
  var rescue_animals_name_id = rescue_animals_name_field.getId();
 
  var rescue_animals_records = rescue_animals_form.getRecords();
 
  for (var index = 0, count = rescue_animals_records.length; index < count; index++){
    var value = rescue_animals_records[index].getFieldValue(rescue_animals_name_id);
    console.log(value);
  }
 
}
 
List_All_Animal_Names();

A quick fix and it works!

Daniel Leu adds: Other syntax for object looping:

for (rec of records) {
  // do something with rec (the record object)
}

for (i in records) {
  // do something with records[i] (the record object at index i)
}

This would make the above for loop code look like this:

for (rec of rescue_animals_records) {
  var value = rec.getFieldValue(rescue_animals_name_id);
  console.log(value);
}

Or:

for (i in rescue_animals_records) {
  var value = rescue_animals_records[i].getFieldValue(rescue_animals_name_id);
  console.log(value);
}

NEXT - Section 4: Creating a Report Record