Looping Through Records

Viewing 5 reply threads
  • Author
    Posts
  • September 7, 2021 at 10:15 AM #45191

    Mark Robbins
    Participant

    I have a need to loop through each record in a table and run a script for that record. I wrote the script below, but it does not seem to work. I’m new to Tap Forms, and a bit new to javascript, so my understanding may be at fault. I know the called script works well when I run it from the Form window for a record, I just can’t get it to run against each record.

    Thanks.

    function Process_All_Records() {

    var records = form.getRecords();
    var field_bggid = “fld-ce86e98498894969839b4d574c754511”
    var value_bggid = 0

    for (thisRecord in records)
    {
    // go to the record and run the function;
    form.selectRecord(records[thisRecord]);
    console.log(“BGGid: ” + records[thisRecord].getFieldValue(field_bggid));
    console.log(“Index: ” + thisRecord);
    form.runScriptNamed(‘Fetch_Bgg_Information’);
    }
    }

    Process_All_Records();

    September 7, 2021 at 11:20 AM #45192

    Brendan
    Keymaster

    Hi Mark,

    What does Fetch_Bgg_Information() do?

    Can you upload your form template?

    The form.selectRecord(record) function is generally just used as sort of the last thing in a script to have Tap Forms select a specific record in the user interface. It’s like if you added a new record in the script and wanted Tap Forms to select that new record after. At least that’s what I had in mind when I wrote that function.

    September 7, 2021 at 11:42 AM #45193

    Mark Robbins
    Participant

    Fetch_Bgg_Information() executes Utils.getTextFromUrl(url) to retrieve an XML file from a web site API, and then parses the text to populate some field values.
    Form template attached (I think).

    Attachments:
    You must be logged in to view attached files.
    September 7, 2021 at 11:45 AM #45195

    Daniel Leu
    Participant

    I treat form.runScriptNamed() like an include in other languages to define constants and functions. So at the beginning of my script, I’d have form.runScriptNamed(‘Fetch_Bgg_Information’); but without any local function calls.

    Then in the loop over records, you would call whatever function you have and provide the current record id as an argument. Maybe something like this:

    for (thisRecord in records) {
       // go to the record and run the function;
       console.log(“BGGid: ” + records[thisRecord].getFieldValue(field_bggid));
       console.log(“Index: ” + thisRecord);
       Fetch_Bgg_Information(records[thisRecord].getId());
    }

    And then in you Fetch_Bgg_Information script you would have the function

    function Fetch_Bgg_Information(myRecordId){
       let myRecord = form.getRecordWithId(myRecordId);
       .....
    }

    Or you could just define let myRecord = records[thisRecord] in the loop and then access myRecord in your function Fetch_Bgg_Information() without using an argument. I prefer to use function call arguments, specially when the function is defined in a different script.

    September 7, 2021 at 12:37 PM #45196

    Mark Robbins
    Participant

    Thanks, Daniel.
    Your examples helped me get through the record looping, and things appear to be working as desired now. I ended up incorporating the original Fetch_Bgg_Information instead of calling it with form.RunScriptNamed(). When I get time for additional testing, I might pull it back out.

    September 7, 2021 at 2:01 PM #45197

    Daniel Leu
    Participant

    Cool! Thank you for letting me know that you got it working!

Viewing 5 reply threads

You must be logged in to reply to this topic.