Thank you for that. I think I’m starting to get it.
To help my understanding I wrote this field script:
var a = record.getFieldValue(a_id);
record.setFieldValue(time_changed_id, Date.now());
document.saveAllChanges();
As expected, when I change the value of a, the time in milliseconds is shown in the other field.
So I added two more fields (and didn’t change the script). When I click between the two new fields (not editing them), the time in milliseconds is still getting updated.
I guess I should be doing something else to ensure the script only runs when a is changed. Could you point me in the right direction?
Thanks again.
Hi Martin,
It’s because when you use the record.getFieldValue() call in a field script and click the Save button on the script, Tap Forms stores a list of field IDs that it references in that script separately on the Script field itself. Then when you change a value in one of those referenced fields, Tap Forms searches through all Script fields to see if that field being edited is referenced. If so, then it executes that script.
That’s how the example works. When you save the Script field, Tap Forms stores the ID of the barcode field being referenced. When you change the barcode value, Tap Forms finds the script that references that field and executes it. The code then fetches the JSON data from the URL specified along with the barcode value and then sets the other fields to contain the data parsed from the JSON response.
Hope that makes sense. It’s really quite a powerful feature.
Thanks!
Brendan
Thank you for that. That makes sense.
How does Tap Forms knows when any of the other fields the script references change?
eg. in the Tap Forms manual there is an example field script which fetches movie data when entering a value into a barcode field. How does Tap Forms know to only run this script when the bar code changes, as you wouldn’t want the api.upcitemdb.com call when the user changes another field.
Thanks
Martin
Field scripts are executed when any of the other fields the script references change.
So if you have a script that references a Checkmark field, then when you click the Checkmark in the form, the script that references that field should execute.
Same on iOS or Mac.
But you can trigger all the scripts to run in all of the records by clicking the Refresh button at the bottom of the records list view. On iOS you can pull down on the records list view to trigger all the Calculation and Script fields to execute.
Form scripts run only when you manually trigger them.
I want to run some JavaScript when a check box is checked on a record (I have a field script that checks the value of the checkbox, if checked it runs the main logic and then unsets the check box).
I was wondering how often a field script is run?
- On every update
- Every time the record in viewed
- On import
Also, does this vary between iOS and MacOS?
Thanks
Martin
Thank you for all your help.
The solution I have gone with is to have a field script that updates the record (from the webscrape). I have also added a check mark to say when I want it to run.
The field script checks the value of the check mark and if set, it runs the web scrape, updates the fields and then unchecked the checkmark.
Thank you
Sure, you could update a record in a Field Script. An example of that might be selecting something from a Pick List that triggers a script to run and it updates a different field in that same record.
You could also have a Saved Search that fetches the records you want to update and run the script just on those records. You would use search.getRecords() instead of form.getRecords(). Or you can ask the form for a specific with a provided name. E.g. var search = form.getSearchNamed('Action & Adventure'); Then do search.getRecords();.
Hi,
Thanks for all your help with the queries I’ve had regarding scripting on iOS.
I’ve found it quite hard to debug on iOS as I can’t see where the console.log() output goes. Is there somewhere I can view the console?
It works in on MacOS (but I rarely have access to my Mac so rely on iOS)
Thanks again,
Marrin
Actually the script does seem to be running, but a second call to alertWithMessage isn’t showing, nor is the final “success” dialog.
I will have to investigate further.
Yes that’s right. It’s good for showing the results of running a script. It’s displayed asynchronously after the script executes.
Good question about whether it is a form script or field script. It’s a form script (would updating a record in a field script make sense?)
I don’t want to loop through and find the record as I am scraping a website (for missing image data) and don’t want to run though all 1000 records and hit the website. On the Mac I was relying on the current record, I guess I can’t do that on iOS.
I will add an extra check box that I will set manually before running the script and then the script can search on that.
Thank you. I have tried:
Utils.alertWithMessage("almost done", record);
And it puts up a dialog box, but the script doesn’t continue after pressing OK.
Is it only intended to be used at the end of a script?
Hi Martin,
The promptForInput function was an old function that is no longer there for Mac or iOS. I inadvertently forgot to update the manual with the new Prompter() class API. I’ve fixed the documentation for that now. But I still have to implement that on the iOS version
The Utils.alertWithMessage('Script Run Complete!', 'Cool!!!!!'); function should work on iOS though. I just tested it and it does work.
Thanks!
Brendan
Hi Martin,
Is this a form script or a Script field?
If it’s a Script field, then record should refer to the current record.
If it’s a form script, then you’ll need to find the record you want to update within the script by searching for the right record by looping through them.
I have a test script that updates a record on MacOS Tap Forms, but it doesn’t update the record on my iPad.
Here is an example:
var field_id = ‘fld-43e292a99a1646f1b5574954169ab319’;
record.setFieldValue(field_id, ‘Martin’);
document.saveAllChanges();
Could you please point me in the right direction to get this working on my iPad (works great on MacOS).
The database was created on the Mac and shared to the iPad, then synchronised with Nearby Sync.
Also, does the concept of current record exist in iPhone scripts?