Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Using Tap Forms › Javascript field question
- This topic has 5 replies, 2 voices, and was last updated 6 years, 1 month ago by Brendan.
-
AuthorPosts
-
September 22, 2018 at 3:07 PM #30712
David ButenhofParticipantWhat I initially expected doesn’t seem to be the way it works, or maybe I just can’t figure it out.
You’ve got both “field” and “form” scripts. The documentation even hints at “field” scripts as an extension of the spreadsheet-like calculation fields; but I wasn’t able to figure out how to set a value for the script field. In a spreadsheet, each cell has both a “formula” and a “value”, and that’s how your calculation fields work as well, but the Javascript fields don’t appear to work that way. The sidebar won’t even let me find a field ID for the script field, so if it’s possible there has to be some trick to it; and simply returning the desired value doesn’t appear to do anything. Is this possible, or do I really need to define both a script field and a value field where the script stores the result?
I also found out that the field script runs only when the record is saved, so adding a “compute total” calculation script field didn’t update the “total” column until I manually modified each record. A simple adaptation in the form of a form script that iterated through the records took care of that … but is it possible to make a form script that iterates over the records and directly calls a script in each record instead of duplicating the code?
September 22, 2018 at 6:02 PM #30721
BrendanKeymasterHi David,
I haven’t written any code to allow a script to call another script. But we did talk about that in the beta forum a while ago.
A Field script does operate the same way a Calculation field works. Whenever the fields that are referenced in the script change, that’s when the script gets executed. Whenever you save a Field script, Tap Forms looks for any
getFieldValue()
calls. It then builds a list of fields that are referenced in that script and it saves it to the field. Whenever you change a value in the record for any of those fields, Tap Forms will execute the script. Whatever the last value is that’s returned from the script will be stored in the database and displayed in the field.Make sure you set the Result Type appropriately to match the value type you’re returning from the Field script, otherwise you won’t see anything. So if you return a string, then set the Result Type to Text, etc.
Hope that helps!
Brendan
September 22, 2018 at 6:30 PM #30725
David ButenhofParticipantThanks.
I tried again and it worked. In retrospect, I suspect my problem the first time was the reflex to close each line with a semicolon, which of course would have meant the field script wasn’t returning a value at all.
Ooops!
But whereas using a separate “total” number field I’d been able to set the field in every record with a form script, that doesn’t work now that I’m using the field script properly. Is there a way to make the script run for each record without actually changing a field, or do I have to do something dumb like adding a space to a text field just to force an update?
September 22, 2018 at 6:50 PM #30726
BrendanKeymasterYes you can run the Script and Calculation fields by clicking the Refresh button at the bottom of the records list. Tap Forms will loop through each record and find any Calculation and Script fields in your form and it will execute them for every record.
You do need to have a semi-colon at the end of each statement. But the last statement should just be the variable or string that you want to return.
For example:
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;
So the last thing in the script is just
'The total is: ' + total;
which is the return value from the script. You would need to set the Result Type toText
. If you just want to returntotal;
then you would leave the Result Type set to the defaultNumber
.Hope that helps!
Brendan
September 22, 2018 at 7:00 PM #30727
David ButenhofParticipantHuh. Yeah, it seems to work with or without the trailing semicolon, so I have no idea why it didn’t work for me the first time I tried. In any case, it does now.
And thanks for pointing out that an explicit refresh would update all the records; that hadn’t occurred to me.
And, by the way, all of this would be great detail to put in the manual, along with a full example of a field script returning a real value.
var movies_views = record.getFieldValue('fld-100d486783794f95a5ba1091e901c85e'); var count = movies_views.length; var old_count = record.getFieldValue('fld-78570a9d833949f6877468ad32aab974'); if (!old_count) {old_count = 0;} var total = count + old_count total;
September 22, 2018 at 7:21 PM #30728
BrendanKeymasterGood idea. I just added my example to the online user manual, along with the other 2 scripts that are there as examples.
Thanks!
Brendan
-
AuthorPosts
You must be logged in to reply to this topic.