Help

Selecting Records with Field does not work with config.

Topic Labels: Automations Extensions
Solved
Jump to Solution
965 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Philipp122
4 - Data Explorer
4 - Data Explorer

Hi, I'm having Problems with a Script, that are very weird.

I'm writing a Script that should pull Crypto Prices from CoinMarketCap. For that, I created a Table "Currencies". In this Table there are 3 Fields (for the sake of this example): "Name", "Symbol", "Price". Now in the Script Settings you can enter your API Key (not relevant here), the Table of the Currencies and a Symbol and Price Field. Now the Script should query all Symbols existing on the Table, compile them into a Comma-Separated List and query the API. Then it should fill in the Price for each Symbol. This is the Code so far:

 

const config = input.config({
    title: 'Crypto Price Importer',
    items: [
        input.config.text('API_KEY', {
            label: 'API KEY'
        }),
        input.config.table('TABLE', {
            label: 'Table',
        }),
        input.config.field('SYMBOL', {
            label: 'Symbol Field',
            parentTable: 'TABLE'
        }),
        input.config.field('PRICE', {
            label: 'Price Field',
            parentTable: 'TABLE'
        })
    ]
});

const selectedTable = config.TABLE;
const selectedSymbolField = config.SYMBOL;
const selectedPriceField = config.PRICE;

const query = selectedTable.selectRecordsAsync({ 
    fields: [selectedSymbolField, selectedPriceField] 
});

const symbolList = (await query).records
    .map(record => record[selectedSymbolField.name])
    .filter(record => record)
    .join(',');

console.log(symbolList)

 

The Problem now is that whenever I try to run this (note there is no API Call yet), it gives me an empty String. Upon some digging, I found that selectRecordsAsync().records only returns the "id" and the "name" Fields of the Records, not the "SYMBOL" and "PRICE" Fields. Honestly I am running out of ideas, maybe somebody can help, or explain why that is. 

1 Solution

Accepted Solutions
Philipp122
4 - Data Explorer
4 - Data Explorer

This has been answered by a Friend who asked ChatGPT (GPT-4) with the Browsing Feature. I have included the AIs answer below for future references.

 

Based on the Airtable documentation and community forum, you should be able to fetch the values of specific fields from a table using the selectRecordsAsync method. However, it requires the fields to be explicitly defined in the method call. In your code, you already have this part correct, where you specify the fields within the selectRecordsAsync method:

 

const query = selectedTable.selectRecordsAsync({ 
    fields: [selectedSymbolField, selectedPriceField] 
});

 

However, when you try to access the field values while iterating through the records, you're trying to use the field objects directly as keys, which isn't the correct way to access the field values in Airtable scripts. Instead, you should use the getCellValueAsString method on each record, passing the name of the field you want to access as an argument.

Here's how you could modify your script to correctly access the field values and construct the comma-separated list of symbols:

In this revised code:

 

const query = await selectedTable.selectRecordsAsync({ 
    fields: [selectedSymbolField.name, selectedPriceField.name] 
});

const symbolList = query.records
    .map(record => record.getCellValueAsString(selectedSymbolField.name))
    .filter(symbol => symbol)
    .join(',');

console.log(symbolList);

 

1. The fields option in selectRecordsAsync is now correctly using the field names instead of the field objects.
2. The map function now calls getCellValueAsString on each record to get the value of the selectedSymbolField.
3. The filter function ensures that only non-empty, non-null symbols are included in the final list.

Make sure that the fields you are trying to access are text fields, as getCellValueAsString will return the value formatted as a string. If the fields are of a different type, you might need to use getCellValue instead and handle the returned value accordingly.

This should resolve the issue of getting an empty string and instead provide you with a comma-separated list of symbols from your table.

See Solution in Thread

1 Reply 1
Philipp122
4 - Data Explorer
4 - Data Explorer

This has been answered by a Friend who asked ChatGPT (GPT-4) with the Browsing Feature. I have included the AIs answer below for future references.

 

Based on the Airtable documentation and community forum, you should be able to fetch the values of specific fields from a table using the selectRecordsAsync method. However, it requires the fields to be explicitly defined in the method call. In your code, you already have this part correct, where you specify the fields within the selectRecordsAsync method:

 

const query = selectedTable.selectRecordsAsync({ 
    fields: [selectedSymbolField, selectedPriceField] 
});

 

However, when you try to access the field values while iterating through the records, you're trying to use the field objects directly as keys, which isn't the correct way to access the field values in Airtable scripts. Instead, you should use the getCellValueAsString method on each record, passing the name of the field you want to access as an argument.

Here's how you could modify your script to correctly access the field values and construct the comma-separated list of symbols:

In this revised code:

 

const query = await selectedTable.selectRecordsAsync({ 
    fields: [selectedSymbolField.name, selectedPriceField.name] 
});

const symbolList = query.records
    .map(record => record.getCellValueAsString(selectedSymbolField.name))
    .filter(symbol => symbol)
    .join(',');

console.log(symbolList);

 

1. The fields option in selectRecordsAsync is now correctly using the field names instead of the field objects.
2. The map function now calls getCellValueAsString on each record to get the value of the selectedSymbolField.
3. The filter function ensures that only non-empty, non-null symbols are included in the final list.

Make sure that the fields you are trying to access are text fields, as getCellValueAsString will return the value formatted as a string. If the fields are of a different type, you might need to use getCellValue instead and handle the returned value accordingly.

This should resolve the issue of getting an empty string and instead provide you with a comma-separated list of symbols from your table.