May 22, 2019 10:31 PM
When attempting to use the PATCH
method in Google Apps Script , I get an error INVALID_REQUEST_UNKNOWN
.
I’m able to do a POST
and GET
request. Tested my credentials using CURL and this worked. Tried the “HTTP Override” hack and that did not work.
function postToAirtable() {
var url = 'https://api.airtable.com/v0/appxs9P0mXsqcXs5R/Outreach';
var data = {
'fields': {
'Task': '<Task Name>',
'Outreacher': '<Person>',
'Month': 'May ',
'Status': 'In Progress',
'Go Live Date': '2016-03-01',
'Copy Card Link': [
'recONM5ul2oaG6yKi'
],
'Label': 'DET'
}
};
var headers = {'Authorization':'Bearer ' + '<API KEY>'};
var options = {
'contentType':'application/json',
'method' : 'PATCH',
'headers': headers,
// Convert the JavaScript object to a JSON string.
'payload' : JSON.stringify(data),
};
//Logger.log(options);
Logger.log(UrlFetchApp.fetch(url, options));
};
Solved! Go to Solution.
May 31, 2019 01:10 PM
Every record in Airtable has a unique record ID. You receive this record ID whenever you perform a GET or POST request.
When doing an update (PUT or PATCH) or delete (DELETE) operation, you must provide the Airtable record ID for the record you are trying to modify. The record ID should be in the URL path after the table name.
For example, if you are trying to do a PATCH on your record with an record ID “rec123ABC987”, your URL would look like:
var url = 'https://api.airtable.com/v0/appxs9P0mXsqcXs5R/Outreach/rec123ABC987';
May 31, 2019 01:10 PM
Every record in Airtable has a unique record ID. You receive this record ID whenever you perform a GET or POST request.
When doing an update (PUT or PATCH) or delete (DELETE) operation, you must provide the Airtable record ID for the record you are trying to modify. The record ID should be in the URL path after the table name.
For example, if you are trying to do a PATCH on your record with an record ID “rec123ABC987”, your URL would look like:
var url = 'https://api.airtable.com/v0/appxs9P0mXsqcXs5R/Outreach/rec123ABC987';
May 31, 2019 02:32 PM
That worked perfect. Thanks for your help
Aug 12, 2019 08:15 PM
hello, can i get some help? how to post to airtable using google app script?
Aug 13, 2019 01:29 PM
The script I used above is how I post to Airtable. I’m not really a developer…I just hacked my way through it. But the basic process I use is to pull all of my data into Google Sheets, then use the script in this thread to push that sheet into Airtable.
Aug 13, 2019 10:35 PM
Here’s a simple script I use to patch updates from Google Apps Script. It’s very similar to the above example.
A key issue that many people run into writing integration code is the need to URI encode anything that appears in the URL. In the example above, “Outreach” is the table name, but if it had spaces (i.e., Outreach Activites), the example code would fail.
Aug 14, 2019 12:09 AM
Thank you Bill, i really appreciate it.
Aug 14, 2019 02:42 AM
@Bill.French, how can i count the record in my table? thanks.
Aug 14, 2019 06:26 AM
To count the number of records in a table using the API unfortunately requires fetching every record in the table into an array and then using the array.length method unless you keep a field of record numbers incremented.
These are each crappy solutions - perhaps someone in the forum knows a better way using native Airtable functions.
The API currently has no count methods, but you can request a dump of the table and constrain the field schema to just one field making the process faster and a bit more optimized.
Aug 14, 2019 06:18 PM
Thanks for the info, last question can i call multiple html page to the script?
Thanks alot for your time answering all my question, i really appreciate you…
Aug 14, 2019 06:25 PM
I’m not entirely sure what you mean by this question. Care to expand your idea a little more?
Aug 14, 2019 10:02 PM
Google script use doGet function to load the htmlserver or the page. when the page was loaded, I want to load another page once i click the next button. Would it be possible?
Aug 15, 2019 08:06 AM
Yes - this is simply a matter of constructing the first (home) page to link to other pages. The “other” pages it links to either have to exist as separately rendered pages through the same doGet() method or they must exist on the web somewhere in a web server.
Another more elegant approach is to use javascript on the client (home page) to update that page dynamically using the DOM (document object model).
Aug 25, 2019 11:32 AM
Is it not possible to make a patch request using filterByFormula instead? I need to be able to make a patch request based on some other value in my table other than the record_ID…
Aug 26, 2019 06:53 AM
The API specifically states that record ID is required to update any Airtable record, so - no - this is not possible.
What is possible is how you implement the patch process. I believe this is exactly what the Airtable.js SDK does, although I do not use it myself. As I recall, the SDK has a method to leverage filter-like processes to narrow down the table items to be updated.
Doing this without the SDK is relatively simple -
To the best of my knowledge …
It’s unfortunate that the Airtable API requires us (all of us, even the SDK) to fetch all records from a table. Just because the SDK has a filterBy() method, it doesn’t mean the SDK is somehow more efficient than my described steps - it still needs to look at all the records before applying a patch.
I hope I am wholly misinformed about this and would rejoice in being wrong. :winking_face: