Simply refresh new Record in background

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Simply refresh new Record in background

Viewing 11 reply threads
  • Author
    Posts
  • January 23, 2022 at 5:31 AM #46387

    Chris Ju
    Participant

    Hello,

    i’m trying to refresh a new child record which i created with ‘record.addNewRecordToField’ in background.

    I just don’t want to double click the new entry and press CMD-R. In the related child form there a some script and calculation fields. The problem is, that calculation 1 has an result for calculation 2 and these both has a result i use in a script field.

    At the moment i have to open the new record and press three times CMD-R. This isn’t so funny that I want to do it 100 times a day…

    I couldn’t find something in the javascript documentation to trigger the refresh with a script…

    Had anyone had the same problem and have a solution?

    Thanks a lot…

    January 23, 2022 at 8:56 AM #46390

    Chris Ju
    Participant

    I just resolved the automatic refresh of all required fields (with all their dependencies) just by changing the order of the fields and adding an if-loop to each of the calculation fields and the script field. Now i can click the “+>”-Button and all fields are refreshed without hitting CMD-R.

    BTW: I had also the idea to merge the calculation fields to the script field, but i don’t know if this will do it (at the moment i haven’t the time to “translate” the formulas to javascript)…

    @Brendan: Isn’t possible to add an function like << record.refreshRecord(recordID) >> which will do the same as CMD-R?

    January 23, 2022 at 10:47 PM #46394

    Sam Moffatt
    Participant

    Chained fields is going to be complicated because how many times does the refresh need to be called to resolve everything becomes a recursion problem. How do you know when to stop evaluating if someone adds say a time based script that mutates each execution.

    A model I’ve used is having a single script that calculates everything and then stores the data in normal fields. There isn’t a read only option so they can be mutated by the end user but you can also work around that by using self referencing script fields that maintain their value set by an external script.

    January 24, 2022 at 12:22 AM #46399

    Chris Ju
    Participant

    Thanks, Sam, but what do mean exactly with

    “you can also work around that by using self referencing script fields that maintain their value set by an external script”?

    Do you have a short example?

    January 24, 2022 at 1:18 AM #46402

    Sam Moffatt
    Participant

    Most of the time you can have an empty script field and it seems to work. There was one situation where for what ever reason it didn’t work for me and my work around was to just have the script do record.getFieldValue(scriptFieldId); which returns the value for itself. Since the value for the field is set by your other script field (e.g. record.setFieldValue(scriptFieldId, desiredFieldValue)) this should work properly. This is at the limits of what a script field should do but it does seem to work properly and gives you a read only field.

    January 24, 2022 at 1:59 AM #46408

    Chris Ju
    Participant

    Ok, wouldn’t have thought it is possible to write into a script field via script. Thanks for the great hint. Will try to implement it in the near future…

    January 24, 2022 at 2:13 AM #46410

    Brendan
    Keymaster

    Hi Chris,

    Yes, I could probably add a refresh function. form.refreshRecord(aRecord); would probably be a better API.

    I’m not sure if this would help you but there is an alternative setFieldValue() function:

    record.setFieldValue(movie_title_id, 'The Terminator', false);

    From the manual:

    Sets the value on the specified field and optionally executes scripts that would run by default when the field value changes.

    It also evaluates formulas too, not just scripts.

    January 24, 2022 at 11:59 PM #46419

    Sam Moffatt
    Participant

    Wouldn’t the false disable executing the script? Isn’t the problem the chaining essentially means that if it’s executed out of order that the value isn’t available from the “earlier” calculations which means that it’ll not have the value required later to work. I think depending on correlated calc fields is probably an antipattern because you’re relying on TF ordering and if there is common code between fields to isolate that into a form script (which you can then include from form.runScriptNamed) so that there is one copy of it you can use elsewhere or setting the different values into empty script fields as noted above.

    January 25, 2022 at 2:14 PM #46436

    Brendan
    Keymaster

    Oh yes, the false does disable it. So just pass in true.

    January 25, 2022 at 6:47 PM #46437

    Daniel Leu
    Participant

    I was wondering about that as well. Brendan, could you please update the documentation as well? Thanks!

    January 25, 2022 at 8:56 PM #46438

    Brendan
    Keymaster

    Ugh… oops. Sorry about this misunderstanding. The default for that flag is already true, so subscripts already get run when you set a value, even without that parameter. Sorry for causing the confusion.

    January 26, 2022 at 12:04 AM #46441

    Daniel Leu
    Participant

    From the documentation: Sets the value on the specified field and optionally executes scripts that would run by default when the field value changes.

    So it should say: …optionally _disables_ scripts that would run by default…?

    Thanks!

    January 26, 2022 at 11:42 AM #46455

    Brendan
    Keymaster

    Well, optionally just means you don’t need that parameter, but setting it to true has no effect from the default and setting it to false disables. The sample code is set to false. I guess your wording is a bit better. Thanks.

Viewing 11 reply threads

You must be logged in to reply to this topic.