How to get record ID of filtered record [solved]

Hi,

I think I’ve figured out how to retrieve a unique record in my table using the following filter, but how can I get the ID of resulting query?

let filteredRecords = query.records.filter(record => {
    let name = record.getCellValue('Order/Shipment ID');
    return name !== null && name === range
    });

I’ve tried the following but am getting an error

let filteredRecordsResult = filteredRecords.records[0];

output.text(filteredRecordsResult.id);

Error: TypeError: Cannot read property ‘0’ of undefined

1 Like

I hope that someone can help

@kuovonne???

Before filtering, the array is query.records. After filtering, the array is just filteredRecord. Thus, you do not need the .records part anymore. That is why you are getting the property undefined error. The filteredRecords object does not have a .records property.


let filteredRecordsResult = filteredRecords[0];

You might also want to check the length of the filtered array just in case all the records are filtered out and you have an empty array.


I also recommend renaming the variable filteredRecordsResult to something like firstFilteredRecord to better reflect its value:

Instead of …

let filteredRecordsResult = filteredRecords.records[0]; 
output.text(filteredRecordsResult.id);

I recommend …

let firstFilteredRecord = filteredRecords.records[0]; 
output.text(firstFilteredRecord.id);

This is a purely stylistic choice and will have no effect on the execution of your code, but it will make it easier for you and and other people to understand later on.

Excellent! Thank you so much. I understand now. I removed the .records and it worked great!

1 Like

Here is my entire code if anybody is interested:

// set the Inventory and Order Events
let inventoryEventsTable = base.getTable('Inventory & Order Events');
let orderGroupTable = base.getTable('Order Group Summary');
let productVariationTable = base.getTable('Products/Variations');

//Select Product
let product = await input.recordAsync('Pick a product to deduct from inventory', productVariationTable);
//Display product
output.text(`You picked ${product.getCellValueAsString("Short Name")}`); 

//Enter quantity
let quantity = await input.textAsync('Enter the number of units you would like to deduct');
//turn quantity into a negative number
quantity = quantity*-1
//Display quantity
output.text(`The quantity that will be deducted: ${quantity}.`);

//choose order group
let orderGroup = await input.recordAsync('Select the order group to be deudcted from',orderGroupTable);

//set today's date
let today = new Date();

//Enter week description
let range = await input.textAsync('Enter this weeks date range of the record you are entering. Example 200322 - 200328');
//display date range entered
output.text(`The date range you entered: ${range}.`);

//update range variable
range = 'MF ' + range + ' - ' + orderGroup.name + ' - ' + product.name;

// if product is not empty show the following text on the console and create the fields. If user opens the 'pick a record' prompt but cancels the script goes down to the 'Else' statement near the end.
if (product) {
      

let recordIds = await inventoryEventsTable.createRecordsAsync([
    {
        fields: {
        //sets the Type column to one of the existing Single Select Values
        'Order Group Summary': [{id: orderGroup.id}],
        //sets the product in the Detail page Resrouces table to the record that was selected with the record input selection
        'Product': [{id: product.id}],
        //set date
        'Date': today,
        //set quantity
        'Transaction Qty': quantity,
        //set transaction type field
        'Event Type (do not modify options)': {name: 'MF Order'},
        // set location field
        'Location/Vendor': [{id: 'recLJYUluoEI6QxqK'}],
        //set Order/Shipment ID with date range
        'Order/Shipment ID': range,
        //set completed checkmark field
        'Received/Completed': true,
        },
    },    
    
]);

let query = await inventoryEventsTable.selectRecordsAsync();

let filteredRecords = query.records.filter(record => {
    let name = record.getCellValue('Order/Shipment ID');
    return name !== null && name === range
    });

let firstFilteredRecord = filteredRecords[0];

output.text('________________________');
output.markdown("**Now find the record you just created and inspect it for accuracy.**");
output.text(`New record id in the Inventory & Order Events table ${firstFilteredRecord.id}`)
output.markdown(`Link to new record: https://airtable.com/tblvP7CNlk7TNoQ7V/viwafNY93fNgbIJjk/${firstFilteredRecord.id}?blocks=bipfS82tutcM3uzNE.`);

}

else {
    output.text("You didn't select a record. Please start again.");
};

Thank you very much to both of you , your article was really helpful

Benito

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.