Frequency of field script execution?

Tagged: 

Viewing 15 reply threads
  • Author
    Posts
  • October 8, 2018 at 2:57 AM #30956

    Martin Kendall
    Participant

    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?

    1. On every update
    2. Every time the record in viewed
    3. On import

    Also, does this vary between iOS and MacOS?

    Thanks

    Martin

    October 8, 2018 at 1:57 PM #30962

    Brendan
    Keymaster

    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.

    October 9, 2018 at 4:46 AM #30969

    Martin Kendall
    Participant

    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

    October 9, 2018 at 11:21 AM #30972

    Brendan
    Keymaster

    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

    October 10, 2018 at 12:23 AM #30980

    Martin Kendall
    Participant

    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.

    October 10, 2018 at 12:37 AM #30981

    Martin Kendall
    Participant

    I’ve done a bit more investigating.

    In the script editor (on the Mac) after saving and then reopening it, there is a check mark against the A field and none of the others. So the script is doing the clever bit (knowing that it should only run on changes to A), but it appears to run on changes to any field.

    I’m using Tap Forms 5.3.2 on the Mac.

    I’ve now just checked on my iPad and iPhone and the script, correctly, only updates on changes to A. So perhaps it is a bug in the Mac version?

    October 10, 2018 at 3:50 PM #30984

    Brendan
    Keymaster

    Yup. You’re right. I’ve just made a change to fix this so it will do what it’s supposed to do. The Mac version was just calling the wrong code that wasn’t evaluating the script only for those fields that were referenced in it. But the next update will behave that way.

    November 25, 2018 at 8:40 PM #32064

    Federico Martinez
    Participant

    if that field being edited is referenced. If so, then it executes that script.

    Hi Brendan,

    But this does not happen if it is edited from another script right? or at least not one in another form?

    right now I was trying to do scripts between 3 forms an independent scoring form, a capture form and a brain form that does what I need it to do. they all work great independently but to see the changes I have to wait until its done and then hit the refresh records for the other scripts to do their things. In my capture form its not a big deal but it is in my scoring form. I am taking advantage of the “link to form join” I can go around this by making more functions on the brain side and duplicating the other scripts. just wanted to make sure this is intended and not a bug.

    Thanks!!

    November 25, 2018 at 9:06 PM #32065

    Brendan
    Keymaster

    Hi Federico,

    The scripts only get executed on the same form they’re referenced from. It happens when you call setFieldValue(field_id, value); I don’t traverse all of the linked fields to execute script fields on them.

    November 26, 2018 at 6:27 AM #32097

    Federico Martinez
    Participant
    var transfers = document.getFormNamed('Transfer requests');
    var pending_id = 'fld-ed603b4c67b14e829a2d96e5d3cab22c';
    var no_match_id = 'fld-7dcda80275e74246bf0bc3bcb5b6db86';
    
    var records = transfers.getRecords();
    
    for (var index = 0, count = records.length; index < count; index++){
      var pend = records[index].getFieldValue(pending_id);
    	
    	if (pend) {	
    	console.log("true");
    	records[index].setFieldValue(no_match_id, true);

    So this won’t execute a script on form “transfer requests”?
    is there a way to call on a script or formula to execute from another script?

    Thanks

    November 26, 2018 at 1:39 PM #32123

    Brendan
    Keymaster

    The scripts will be executed that reference the no_match_id field. But it won’t go further than that in the hierarchy.

    So whenever you call setFieldValue(), whatever field you pass in will have the scripts that reference that field executed. Tap Forms knows which scripts reference a field if you use the getFieldValue function for the specified field. Whenever you save a script that has getFieldValue(), Tap Forms will record the field that was referenced into a separate array stored on the script field itself. Then when you modify any of those fields, Tap Forms will look at all of the script fields in that form to see if there are any that reference the modified field. If so, then it executes that script field.

    November 26, 2018 at 1:59 PM #32126

    Federico Martinez
    Participant

    yes but even if its in another form? I’m sorry for my insistence, right now its not happening when the script is in a different form without hitting refresh..

    1) records before running script in another form “chain” SS1
    2) Script in form “chain” SS2
    3) records after running script in form “chain” SS3
    4) records after hitting refresh in form “transfer requests” SS4 (how I think it should end up in SS3 without hitting refresh)
    5) Script in form “transfer requests” referencing the same no_match ending in “db86” which should be running without hitting refresh? SS5

    Attachments:
    You must be logged in to view attached files.
    November 26, 2018 at 2:37 PM #32135

    Brendan
    Keymaster

    Would you be able to email me a Tap Forms Archive of your parent form so that I could investigate this further? Send to support@tapforms.com.

    I’ll have to step through my debugger to see exactly what’s happening in this situation.

    If you click a different record and then click back without hitting the refresh button, does the screen update with the new record? I’m not sure how you have your relationships setup. Are they Join fields?

    November 26, 2018 at 3:40 PM #32138

    Federico Martinez
    Participant

    Sent, thanks for checking.

    it does not update when clicking different records without hitting refresh.
    the relationship is setup as join.

    December 1, 2018 at 9:26 PM #32338

    David Sieber
    Participant

    Brendan, you mentioned an update in an earlier message on this thread.

    Has it been released?

    I am using the script feature and I’m having trouble with updates not happening when I expect them to.

    In fact, if I hit Command-R to refresh the view, updates on my script fields stop happening completely, until I exit the app and relaunch it. Then they work until I do the next Command-R.

    I’m running 5.3.4 Build 937 on macOS 10.14.1.

    Thanks,

    –david

    December 2, 2018 at 1:00 PM #32346

    Brendan
    Keymaster

    Hi David,

    I have not released my next update just yet. It will be version 5.3.5.

    Thanks,

    Brendan

    December 2, 2018 at 11:47 PM #32355

    David Sieber
    Participant

    Thanks, Brendan. Allow me to cast my vote for the new release ASAP :)

    —david

Viewing 15 reply threads

You must be logged in to reply to this topic.