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

Section 2: Ugly and Inefficient Code

You recall from Tap Forms Scripting 101, or your previous object oriented programming experience, that an object has properties and functions (also known as methods). Functions have () after them while a property doesn't.

var resultsOfFunction = object.myObjectFunction();
var contentsOfProperty = object.myObjectProperty;
 
// functions and properties can also be called within a line of code: console.log(object.myObjectFunction());
console.log(object.myObjectProperty);

If you look at the Tap Forms documentation, you see that a document has a getForms() function and the form object has a property name.

We can get an array of form objects from the document object. You may recall an array as a list of things that you reference by their index.

Here's how to list the forms in our database:

First, we get an array of form objects:

var forms = document.getForms();

Then, we loop through the array (using the modified Basic loop snippet). If a method returns an array, you're likely going to want a loop to do things with/to the things in the array. You recall that the length property of an array returns the number of elements, which are indexed starting at 0.

for (var index = 0, count = forms.length; index < count; index++){

Then we get the name property of the form at the index:

console.log(forms[index].name);

Loop to the next index.

}

We could also get the form objects directly from our array:

Or by just using the document and form objects directly:

Note this section's title about ugly and very inefficient code... General rule: If you are going to access the result of an object property or function more than once, use a variable. It's more readable AND faster execution. The first way, with the loop is correct. One call to document.getForms() and you don't have to change the code if you add or delete a form.

These alternatives to the same output are there to show that you are using objects and accessing their methods and properties (which were listed in the Tap Forms documentation).

Let's get back to the simple document model. Don't worry if things don't make sense yet. We'll go through this information a few different ways.

Here are some lines of code that would access these objects. Explanations and more efficient record access syntax are upcoming. The important thing to notice here is that objects have a relationship to each other and are being accessed by variables that represent the objects.

A document contains forms.

A form is part of the document and contains records.

A record contains fields with their associated values.

A field is part of a record and has an associated value.

Technically, a value is a property of the field object, but it may return an object depending on the field type.

VBA:
Tap Forms MS Access MS Excel SQL
.getRecords() Recordset Range.Cells SELECT * FROM tbl;

To get or set a value, you need the associated field, which record the field belongs to, and which form the record belongs to.

With an understanding of the objects and how they relate, we can start retrieving our data. Here is the documentation for the next example.

If we want to get the name of the animal of the first record of the Rescue Animals form, we can simply do this:

var field_value = document.getFormNamed('Rescue Animals').getRecords()[0].getFieldValue(document.getFormNamed('Rescue Animals').getFieldNamed('Animal Name').getId());

Yeah. Apologies for my programmer humor... Don't ever write code like that. Ugly and very inefficient.

However...

It works, because you are applying the object model correctly. If an object's method returns an object, you can call a method on that object using the .method() syntax.

var result = object.method().resultingObjectMethod().thatResultingObjectMethodsMethod();

Here's how that obnoxiously long and inefficient code works. The better, easier (correct!) way is coming next.

That graphic looks like it belongs in a circus with a person in stilts passing out popcorn. That's where it should stay, too.

If we really want to retrieve a value, the code gets easier to read (and only makes 1 call to getFormNamed() if we put those objects into variables.

Each line of code in this example is retrieving something from the Tap Forms database using the object model. We retrieve the form object that represents the 'Rescue Animals' form using the document object. We use that form to retrieve the field object that represents the 'Animal Name' field on our form. We use the field's getId() function to retrieve that field's ID. We use the form object to retrieve an array of the form's records. We then grab the first record object (at index 0). Then we use the record's getFieldValue() function with the field's ID to get the field's value.

Here's another to look at this code (if you already understand, skip to the end of the page).

function Better_This_Way() {

document.getFormNamed('form name') returns a form object.

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

form.getFieldNamed('field name') returns a field object.

  var rescue_animals_name_field = form_rescue_animals.getFieldNamed('Animal Name');

field.getId() returns a string that is the ID.

  var rescue_animals_name_id = rescue_animals_name_field.getId();

form.getRecords() returns an array of record objects.

  var records_rescue_animals = form_rescue_animals.getRecords();

array.[0] returns the first element of the array.

  var rescue_animal_record = records_rescue_animals[0];

Which, in this case, is our first record object on the form object.

record.getFieldValue(Id) returns the value of the field on that record.

  var field_value = rescue_animal_record.getFieldValue(rescue_animals_name_id);

Then we simply print that value to the console.

  console.log(field_value);
 
}
 
Better_This_Way();

The code works using Tap Forms' objects and their functions and properties. We can assign the objects to variables to make code easy to read and more efficient.

NEXT - Section 3: Looping through Records