Form Script works, Field Script doesn’t

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Form Script works, Field Script doesn’t

Viewing 7 reply threads
  • Author
    Posts
  • November 22, 2019 at 4:28 PM #38193

    Martin Inchley
    Participant

    The following script is designed to put the current date into a Payment Date field when a Paid tick-box is checked.

    var payment_date_id = 'fld-cf95ec11043d4d2b8dc51d2ab93f177b';
    var paid_id = 'fld-4f9f9ac525964be99ca478bdaee47144';
    var paid = record.getFieldValue(paid_id);
    
    var thisDate = new Date();
     
    if (paid) {
    	console.log(thisDate);
    	record.setFieldValue(payment_date_id, thisDate);
    } else {
        console.log("no date");
        record.setFieldValue(payment_date_id, "");
    }

    When it’s run as a Form Script, it works fine. When it’s used within a Script Field, the console entries come in as usual, but the Date field doesn’t change, neither to get the Date nor to be emptied when unchecking the field.

    If I set the If section to show the output in the Script field itself:

    if (paid) {
    	console.log(thisDate);
    	thisDate;
    } else {
        console.log("no date");
        "";
    }

    the date appears and disappears as it should.

    Am I missing something in how Script fields execute actions?

    November 22, 2019 at 4:55 PM #38194

    Martin Inchley
    Participant

    Extra evidence:

    Tinkering with this I set up a Calc field to hold 1 if the Payment Date field was not empty, and 0 if it was. After the Script field has been triggered by checking the Paid tick-box, this new Calc field is registering 1, even though the Payment Date field appears to have no content.

    There would appear to be something in it, stirring beneath the waters…

    November 22, 2019 at 5:25 PM #38196

    Martin Inchley
    Participant

    Late night extra extra:

    In this scenario, with the Paid box checked, the Calc field showing the Paid Date field is not empty, but the Paid Date field not actually showing anything – if I run a script to fetch the contents of the Paid Date field, the date suddenly shows up in it!

    Similarly when I’m emptying it – the emptying only shows when I run a script to get the contents (and, worryingly, occasionally not even then).

    Is this likely to be a time-lag in saving the data, or is there perhaps some trigger that’s not firing?

    November 22, 2019 at 5:56 PM #38197

    Daniel Leu
    Participant

    I think you are missing a document.saveAllChanges(); at the end of the script.

    November 22, 2019 at 6:05 PM #38198

    Martin Inchley
    Participant

    Good spot, Daniel!

    Unfortunately, adding it made no difference.:(

    And the other script that makes the value appear when I trigger it manually makes no difference when I call it from within my Script Field script.

    November 22, 2019 at 6:11 PM #38199

    Martin Inchley
    Participant

    Well now…

    I’ve set up a custom layout, and it’s all working as it should. It’s only on the default layout that the problem exists.

    It also works fine when I access these fields in a Link-to-form field in a different form. So the workarounds are all in place.

    But I would love to know what’s going wrong when I’m on the default layout.

    November 22, 2019 at 6:49 PM #38200

    Sam Moffatt
    Participant

    Hit the manual refresh button at the bottom of the default layout. Every so often it doesn’t update properly for some stuff and hitting the refresh button recalcs everything and triggers a UI refresh. I find that sometimes that’s enough to reset something somewhere and then things start behaving the right way.

    November 22, 2019 at 6:51 PM #38201

    Martin Inchley
    Participant

    Thanks, Sam. Useful to know.

Viewing 7 reply threads

You must be logged in to reply to this topic.