Apr 25, 2024 02:51 AM
Hi
I am trying to use below code (Google Apps Script) to get data from Airtable. The fetch request succeeds but the fields are returned as text [Object]. I am not able to see actual data. However the request works from other HTTP clients like Bruno.
I have observed another issue. The code with small change marked below used to return data perfectly till couple of days ago but suddenly started throwing error 422. Hence I made below change and the result is missing data.
Even GET method results same.
Please suggest how this can be resolved as this is a show stopper.
Currently the result is like below
{ id: 'recrR654MViYPpGG5', createdTime: '2024-01-30T10:38:29.000Z', fields: [Object] }
My code is as below
let invoicenumin = "EI000002"
const scriptProperties = PropertiesService.getScriptProperties();
airdbKey = scriptProperties.getProperty('airdbkey')
const fieldObj = [
"quantity",
"unitprice",
"lineamount",
"itemdescription"
]
let filter = "{invoicenum} = " + '\''+invoicenumin +'\''
const atdata = {
//"records": [{"fields": fieldObj}] /* this used to WORK EARLIER, BUT STOPPED SUDDENLY AND NOW THROWING ERROR 422*/
"fields": fieldObj,
"filterByFormula": filter
}
const options =
{
"contentType": 'application/json',
"headers" : { "Authorization": "Bearer "+airdbKey },
"method" : 'POST',
'payload' : JSON.stringify(atdata)
};
const API_URL ='https://api.airtable.com/v0/<myappid>/Invoiceline/listRecords'
console.log('Options are', options)
const retval = UrlFetchApp.fetch(API_URL,options)
Solved! Go to Solution.
Apr 25, 2024 05:10 AM - edited Apr 25, 2024 05:16 AM
I think in the above code
console.log('records:', records)
In the above code if I replace console.log('records:', records) with
Apr 25, 2024 03:44 AM
Here's some code that works with Google Apps Script that filters for records where the field "Colour" is "Blue", and only returns the values for the fields "Colour" and "Category", and presents the
The API docs for using the query parameters are quite useful, and the API encoder Airtable provides is also super handy for this:
https://airtable.com/developers/web/api/list-records
https://codepen.io/airtable/full/MeXqOg
const api_key = 'YOUR_PAT_HERE'
const url = 'https://api.airtable.com/v0/appz6YcyO3dXfqDWV/tblzlSMWTq31SyL4C?fields%5B%5D=Colour&fields%5B%5D=Category&filterByFormula=%7BColour%7D%3D%22Blue%22'
function get(){
let records = requestAirtable(url)
console.log('records:', records)
}
function requestAirtable(url, offset) {
if(offset!== undefined){
url = url + '?offset=' + offset
}
let headers ={
'Authorization' : 'Bearer ' + api_key,
'Content-Type' : 'application/json'
}
let options={
headers : headers,
method : 'GET',
}
let response = UrlFetchApp.fetch(url,options).getContentText();
let result = JSON.parse(response)
console.log('result: ',result)
return result.records
}
Apr 25, 2024 04:10 AM
Hi
Thanks for your response. Here what is offset in the above code. If I remove offset and try the code with my airtable details in url along with view name instead of filter by formula (as GET being used) I am getting error 422.
Apr 25, 2024 04:16 AM
Apologies, I am not getting error 422, but instead of actual data I am getting [Object] when the column is a lookup field, earlier it used return actual value within an array
Apr 25, 2024 05:10 AM - edited Apr 25, 2024 05:16 AM
I think in the above code
console.log('records:', records)
In the above code if I replace console.log('records:', records) with
Apr 25, 2024 06:52 AM
It sounds like you're encountering an issue with the Airtable API where the listRecords endpoint is not returning the expected fields data. There could be a few reasons for this issue:
Field Permissions: Make sure that the API key you're using has permission to access the fields you're trying to retrieve. If the fields are restricted, the API may not return their data.
Field Visibility: Check if the fields you're trying to access are visible to the view you're using with the API. If they're hidden in the view, they won't be returned in the API response.
Field Names: Ensure that you're using the correct field names in your API request. Field names are case-sensitive and must match exactly what's defined in your Airtable base.
Authentication: Double-check your authentication method (API key or OAuth token) to ensure it's correctly included in the API request headers