Getting field script to work

Viewing 4 reply threads
  • Author
    Posts
  • August 6, 2020 at 6:14 PM #41600

    Nigel Hughes
    Participant

    Hi,

    I’m trying to get field scripts to work (on iOS) and having little success. To simplify things I’ve copied some code from https://www.tapforms.com/help-mac/5.3/en/topic/scripts but can’t get it to output anything (when Number1/2 is changed). The exact code I’ve entered for the Number3 script field is:

    function Number3() {

    var Number1_field = form.getFieldNamed(‘Number1’);
    var Number2_field = form.getFieldNamed(‘Number2’);
    var Number1_ID = Number1_field.getId();
    var Number2_ID = Number2_field.getId();
    var Number1 = record.getFieldValue(Number1_ID);
    var Number2 = record.getFieldValue(Number2_ID);

    var Number3 = Number1 * Number2;
    ‘The total is: ‘ + Number3;
    form.saveAllChanges();
    }

    Number3();

    I’ve attached a screenshot of the form setup. Can anyone help me to get Number3 to change its value based on Number1/2?
    Thanks alot.

    Attachments:
    You must be logged in to view attached files.
    August 6, 2020 at 9:33 PM #41605

    Sam Moffatt
    Participant

    You need to put a return statement at the end of the function, change the form.saveAllChanges() line to be return Number3; and it should work.

    August 6, 2020 at 10:31 PM #41607

    Nigel Hughes
    Participant

    Hi Sam,
    Thanks so much for responding. However still the value of the Number3 field in the record is not changing when the Number1/2 fields are changed. Just for clarity my code now is:
    function Number3() {

    var Number1_field = form.getFieldNamed(‘Number1’);
    var Number2_field = form.getFieldNamed(‘Number2’);
    var Number1_ID = Number1_field.getId();
    var Number2_ID = Number2_field.getId();
    var Number1 = record.getFieldValue(Number1_ID);
    var Number2 = record.getFieldValue(Number2_ID);

    var Number3 = Number1 * Number2;
    return Number3;
    }

    Number3();

    (I changed the the expected value of the Number3 script field to be a number.) Any other ideas?

    Thanks again …

    August 7, 2020 at 11:12 PM #41609

    Sam Moffatt
    Participant

    The reason it doesn’t autoupdate is because you’re obscuring the field ID’s and Tap Forms doesn’t know which field to watch. If you pull down on the record to recalculate the fields, it should update. Tap Forms is coded to look for the two placeholder templates and will use that to setup the links when those fields are changed.

    In the script editor, there is a button at the bottom that will let you open a field list:

    In this field list you’re given two options at the top, I’d go for the left one and then tap on the field you want. The right one will work but for this simple use case, I’d personally pick the left one:

    That will put the getFieldValue call directly into your script and you can use it from there:

    In your case it should simplify your script down to about three lines inside the function to just directly retrieve the field values and return their multiplied value.

    Attachments:
    You must be logged in to view attached files.
    August 13, 2020 at 6:13 PM #41669

    Nigel Hughes
    Participant

    Thanks!

Viewing 4 reply threads

You must be logged in to reply to this topic.