Form Scripts

In addition to Script Fields, Tap Forms Pro also supports form scripts. These types of scripts are used to perform computations on all of the records in the selected form. Unlike Field Scripts, Form Scripts will not automatically run when you change the data in your records. You must manually run your Form scripts.

To create and modify Form scripts, click the Scripts tab in the form inspector panel and then click the + button to add a new script.

Document Scripts

Document Scripts are basically the same as Form scripts, but they will be available to you on every form within the same document. This can be useful if you have some functions that you want to be able to reference from within any of your other Form level scripts.

You can promote a Form script to be a Document script by clicking the gear button and selecting Move to Document or Move to Form.

Built-in Tap Forms specific objects

A Tap Forms database consists of a Document, Forms, Searches, Fields, Pick Lists, Categories, Records and values. A few of these things can be accessed right from within a script using the following built-in objects without having to declare them:

  • document
  • form
  • search
  • record

These objects represent the currently selected thing. That is, document is the currently opened Tap Forms document. form, search, and record represent the currently selected objects respectively.

So if for example you wanted to get the name of the currently selected form, you could do this:

var formName = form.name;

or to get the ID of the currently selected record:

var recordID = record.getId();

See the Tap Forms JavaScript API to find out what specific functions are available to you.

Snippets

Within the Tap Forms Script Editor, there is a list of Snippets that you can use to help you get access to the records and values that you will need when writing your scripts. For example, to get access to the list of records in a form, use the following line of code:

var records = form.getRecords();

Once you’ve defined the records variable, you can then write code to loop through the array of record objects, picking out values from specific fields in your form. Tap Forms makes this easy by automatically writing code for you when you double-click on a field name and double-click on a Snippet.

For example, here’s a block of code for a form level script which loops through all the records in a form and totals up the revenue from all of the movies in a Movies form:

var records = form.getRecords();
var revenue_total = 0;

for (var index = 0, count = records.length; index < count; index++){
   var theRec = records[index];
   var movie_revenue = theRec.getFieldValue('fld-987c9aac738a4f0fa1d18395902b3fc1');
   if (movie_revenue) {
      revenue_total += movie_revenue;
      console.log('Revenue: ' + movie_revenue);
   }
}

revenue_total;

You may also notice there’s a line of code that says console.log('Revenue: ' + movie_revenue);. That tells Tap Forms to output that line to the console log area on the Script Editor screen. This is very useful for debugging your scripts as Tap Forms doesn’t have a formal JavaScript debugger built-in. You will need to use the console.log() function to aid in debugging.

Fetch Movie Data from Web Service

Here’s an example Field script which fetches movie data when entering a value into a barcode field. You can even use it to scan a barcode using the iOS version. It will automatically fetch the data and fill in the fields for you, including downloading a movie case cover photo.

var movie_title_id = 'fld-987c9aac738a4f0fa1d18395902b3fb1';
var genre_id = 'fld-2a0d16d2aece4562b6f44170cf1736a6';
var case_cover_id = 'fld-e2cf02f376864a7cb9f090e4a597f3e4';
var summary_id = 'fld-28cc8f7f082c43818f1169c96c96e5a9';
var imdb_website_id = 'fld-9bc90613e8db45fea77579f35eb53b9d';
var barcode_field_id = 'fld-7e3ead95de51427fa5a467b2476d58cd';

function fetchProductInfo() {
	var barcode = record.getFieldValue(barcode_field_id);
	var url = 'https://api.upcitemdb.com/prod/trial/lookup?upc=' + barcode;
	var product_info = Utils.getJsonFromUrl(url);
	return product_info;
}

var product = fetchProductInfo();
var code = product['code'];

if (code == 'OK') {
	var items = product['items'];
	var item = items[0];
	var images = item['images'];
	var imageUrl = images[0];
	var offers = item['offers'];
	var offer = offers[0];
	var link = offer['link'];
	record.setFieldValue(imdb_website_id, link);
	record.setFieldValue(movie_title_id, item['title']);
	record.setFieldValue(summary_id, item['description']);

	if (imageUrl != undefined) {
		record.addPhotoFromUrlToField(imageUrl, case_cover_id);
	}
	form.saveAllChanges();
}

And here’s another Field script which returns a simple value by multiplying 2 values together and returning the result:

var amount_id = 'fld.....';
var quantity_id = 'fld....';
var amount = record.getFieldValue(amount_id);
var quantity = record.getFieldValue(quantity_id);
var total = amount * quantity;
'The total is: ' + total;

In the above script, because it returns a Text value, make sure you set the Result Type to Text.

Last modified: Dec 26, 2024

Need more help with this?
Don’t hesitate to contact us here.

Was this helpful?

Yes No
You indicated this topic was not helpful to you ...
Could you please leave a comment telling us why? Thank you!
Thanks for your feedback.