Add record and update values in tables

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Add record and update values in tables

Viewing 12 reply threads
  • Author
    Posts
  • January 20, 2024 at 9:33 AM #50348

    Ray Robillard
    Participant

    I have a movie database in which I’d like to keep an history of every time I watch my movies.  I have a field in which I save the last viewing date.  With a script, I was hoping to copy this value to an history table field.

    So far, I am able to get the current watched date (console.log shows that I am ok there), add a blank record to my table (I can see that when I run the script manually) but I am at a loss to update this newly added record and trigger the script automatically.

    Here’s the script :

    function Update_Last_Viewed_Date() {
         var watched_date_id = ‘fld-0d50c9c744b14f89bcfcaa1295650845’;
         var date_to_log = record.getFieldValue(watched_date_id);
         var viewings_id = ‘fld-edf6c517ee814c059b9abe34846eb65c’;
         var newRecord = record.addNewRecordToField(viewings_id);
         var date_id = ‘fld-da18c2d927f443a3a5cac5b35f705aff’;
         record.setFieldValue(date_id, date_to_log);
         document.saveAllChanges();
    }
    Update_Last_Viewed_Date();
    How can I update the recently added record ?  And, most importantly, how do I trigger this script ?  I read that it should be triggered automatically since I used “getfieldvalue” of the last viewing date.  But it doesn’t.  If I input a new value in that field, it doesn’t call the script (because no blank record is added to the table).

    I added screen captures to better explain what I am trying to do.

    Thanks  in advance !

    Attachments:
    You must be logged in to view attached files.
    January 20, 2024 at 10:03 AM #50351

    Daniel Leu
    Participant

    With addNewRecordToField() you receive a new record object. Now use this to set the values like newRecord.setFieldValue(date_id, date_to_log).

    January 20, 2024 at 11:15 AM #50352

    Ray Robillard
    Participant

    newRecord.. Of course !!  Thanks, new record added and contains the desired value.

     

    Now, how can I have this script triggered automatically ?

    January 20, 2024 at 4:31 PM #50353

    Daniel Leu
    Participant

    Make it a field script instead of a form script.

    January 21, 2024 at 8:32 AM #50355

    Ray Robillard
    Participant

    Got it, thanks!  It’s working now, but there seems to be a bug, or something, the script is executing twice, resulting in two rows being inserted in the table, with the same values (the date I input).  Here’s my script :

     

    function Update_Last_Viewed_Date() {

        var watched_date_id = ‘fld-0d50c9c744b14f89bcfcaa1295650845’;
        var date_to_log = record.getFieldValue(watched_date_id);

        var viewings_id = ‘fld-edf6c517ee814c059b9abe34846eb65c’;
        var newRecord = record.addNewRecordToField(viewings_id);
        var date_id = ‘fld-da18c2d927f443a3a5cac5b35f705aff’;

        newRecord.setFieldValue(date_id, date_to_log);

        document.saveAllChanges();

    }

    Update_Last_Viewed_Date();

    I checked to make sure, the script exists only in one entry, amongst the list of fields.  I also did different tests (empty viewing history list, list with one date already existing, quit and relaunch Tap Forms) and every time I input a new viewed date, it gets inserted twice.

    Am I doing something wrong ?

    Attachments:
    You must be logged in to view attached files.
    January 21, 2024 at 9:16 AM #50357

    Ray Robillard
    Participant

    Saw your explanations on this thread, but it didn’t work for me.  I was able to change two var for const with getId(), and now the script won’t auto execute…

     

    Source : https://www.tapforms.com/forums/topic/pre-processing-field-entry/

    function Update_Last_Viewed_Date() {
        const watched_date_id = form.getFieldNamed(‘Watched date’).getId();
        var date_to_log = record.getFieldValue(watched_date_id);
        const viewings_id = form.getFieldNamed(‘Visionnements’).getId();
        var newRecord = record.addNewRecordToField(viewings_id);
        var date_id = ‘fld-da18c2d927f443a3a5cac5b35f705aff’;
        newRecord.setFieldValue(date_id, date_to_log);
        document.saveAllChanges();
    }
    Update_Last_Viewed_Date();
    If I execute it manually, I get one row inserted with the correct value.
    January 21, 2024 at 9:33 AM #50358

    Ray Robillard
    Participant

    Ok, found the bug.  My initial script works perfectly.

    If you input the date manually (with the keyboard), the script is called twice.  If you use the date picker, however, it’s only executed once.

    Where do I file a bug report ?

     

    January 22, 2024 at 12:01 AM #50365

    Daniel Leu
    Participant

    I just tried with my own script and it works as expected:

    function Update_Table() {
    console.log("Add entry");
    const table_id = "fld-xxx";
    const table__date_id = "fld-xxx";
    const table__name_id = "fld-xxx";
    const name_id = "fld-xxx";
    const date_id = 'fld-xxx';
    let date = record.getFieldValue(date_id);
    console.log("   Date: " + date);
    let recTable = record.addNewRecordToField(table_id);
    recTable.setFieldValue(table__date_id, date);
    document.saveAllChanges();
    }
    Update_Table();
    Using double quotes prevents the field script to be executed.
    • This reply was modified 11 months ago by Daniel Leu.
    January 24, 2024 at 5:06 AM #50371

    Ray Robillard
    Participant

    With my current script :

    function Update_Last_Viewed_Date() {
    var watched_date_id = ‘fld-0d50c9c744b14f89bcfcaa1295650845’;
    var date_to_log = record.getFieldValue(watched_date_id);
    var viewings_id = ‘fld-edf6c517ee814c059b9abe34846eb65c’;
    var newRecord = record.addNewRecordToField(viewings_id);
    var date_id = ‘fld-da18c2d927f443a3a5cac5b35f705aff’;
    newRecord.setFieldValue(date_id, date_to_log);
    document.saveAllChanges();
    }
    Update_Last_Viewed_Date();

    The update works fine, except when I enter the date manually, the script is fired twice.  See the video.

    To me, this is a bug.

    Attachments:
    You must be logged in to view attached files.
    January 24, 2024 at 8:44 AM #50373

    Daniel Leu
    Participant

    Something for @brendan to look at. He’s the developer, I’m just a user. Can you share your document? Maybe send it to support@tapforms.com or attach it here.

    January 25, 2024 at 5:17 AM #50375

    Ray Robillard
    Participant

    I will sent a shared link to support@tapforms.com, the archive file being 126.6 MB.

     

    Thanks !

    January 26, 2024 at 11:00 AM #50384

    Brendan
    Keymaster

    Hi Ray,

    Sorry I haven’t had a chance to look at the file you sent me just yet. But I will.

    Thanks,

    Brendan

    January 26, 2024 at 11:32 AM #50385

    Ray Robillard
    Participant

    It’s ok because there’s a functional workaround, take all your time.

Viewing 12 reply threads

You must be logged in to reply to this topic.