Help

Scripting: Looking For Help On How To Properly Use query.records.filter

Topic Labels: Automations
Solved
Jump to Solution
264 4
cancel
Showing results for 
Search instead for 
Did you mean: 
clabonty
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello - full disclosure, I've only just started to get into scripting in Airtable, so bare with me as I'm still learning!

My actual goal with this script is actually quite complex, I'm probably only about half way through. But instead of trying to summarize the entire thing, I'm just going to hone in on where I'm stuck specifically... which is how to properly utilize the Filter function with arrays.

So I have a button trigger in a table "TV Finishing" that I want to execute a script. The script copies a name and an alphanumeric code (ISCI code) from the record, and pastes it as a new record in the table "AutoHelper". This I have working just fine.

But then I want the script to count how many existing records in "AutoHelper" already have the same ISCI Code, and to paste the number (plus 1) into a number field of the newly created record... and this is where I'm stuck.
I've added my existing script below with the error code I'm getting when I try to use query.records.filter. I've tried to emulate my script off of others I found from the community here, unfortunately I can't seem to figure out what I'm doing wrong exactly.

Really appreciate any help or insight anyone can share!

 

 

// Establishing tables and views
let table = base.getTable("TV Finishing");
let table2 = base.getTable("AutoHelper")
let view = table2.getView("ISCI Gen")

// This script is run from a button field and will use the button's record.
let record = await input.recordAsync('Select a record to use', table);
if (record) {

    // Establishing vales to copy from table to table2
    let slate = record.getCellValueAsString("SLATE TITLE");
    let isci1 = record.getCellValueAsString("ISCI Code Form1");

    // These should be commented out after the script is working properly
    output.text(`The Slate Title copied is: ${slate}`);
    output.text(`The ISCI code copied is: ${isci1}`);

    // Assign query to the array of existing values in "Coped ISCI Code Form1"
    // Filter the array to only the records that match the ISCI code of the new record
    // Assign recordCount to the length of the filtered array, plus 1
    let query = await view.selectRecordsAsync({fields: ["Copied ISCI Code Form1"]});
    let queryMatch = query.records.filter(record => (record.getCellValueAsString("Copied ISCI Code Form1") == isci1));
    let recordCount = Number(queryMatch.records.length)+1;

    // This should be commented out after the script is working properly
    output.text(`The number of records with matching ISCI is: ${recordCount}`); 
    await table.updateRecordAsync(record,{"Button Test": isci1});

    // Create a new record in table2 with the slate and ISCI code copied from table, and the number of existing records with the same ISCI
    let recordID = await table2.createRecordAsync({
        "Name": slate,
        "Copied ISCI Code Form1": isci1,
        "Unique Increment": recordCount
    });

} else {
    output.text('No record was selected');
}

 

 

ERROR

TypeError: Cannot read properties of undefined (reading 'length')
    at main on line 24
1 Solution

Accepted Solutions
dilipborad
8 - Airtable Astronomer
8 - Airtable Astronomer

Hello @clabonty 

At this line 

let recordCount = Number(queryMatch.records.length)+1;

Only queryMatch has that response. there is no element like "records" inside queryMatch.

Just use this, Remove records.

let recordCount = Number(queryMatch.length)+1;

 👍

See Solution in Thread

4 Replies 4
Dan_Montoya
Community Manager
Community Manager

try putting a

consol.log("queryMatch : ", queryMatch);

right before your recordcount line. It looks like nothing is getting returned.

Thanks @Dan_Montoya, that seems to be yielding the results I was hoping for queryMatch.

CONSOLE.LOG

  1. "queryMatch : "
  1. (3) [Object, Object, Object]

The data I tested with has 3 existing matches in the table currently. So it seems that line is working properly... but I'm still getting the same error on this line:

 

let recordCount = Number(queryMatch.records.length)+1;

 

I'm not sure what could be wrong with it, before I inserted the filter in the line before this line was working properly to just report the count of all records. It was this previously:

 

let recordCount = Number(query.records.length)+1;

 

dilipborad
8 - Airtable Astronomer
8 - Airtable Astronomer

Hello @clabonty 

At this line 

let recordCount = Number(queryMatch.records.length)+1;

Only queryMatch has that response. there is no element like "records" inside queryMatch.

Just use this, Remove records.

let recordCount = Number(queryMatch.length)+1;

 👍

Thank you @dilipborad!

It's working as expected now 🙂