Help

Re: Using a Script to Create a record in another base

8451 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Josh_Kurz
4 - Data Explorer
4 - Data Explorer

Question for the community: is it possible to write a script that creates a new record in a table from a different base?
Basically I would like to create a button in a few different tables from various bases that, when triggered, runs a script that creates a new record in a table (with some specific fields copied over) in a separate base.

Is something like this possible in Airtable?
Thanks!
-Josh

9 Replies 9

Hi @Josh_Kurz - you can’t do this directly/wholly in the scripting app as the scope of this app is the base that the script runs in. However, you could have a script in Base A use the REST API to create a record in Base B.

IT_Departmenrt
5 - Automation Enthusiast
5 - Automation Enthusiast

This is was I’m trying to achieve also, do you have any form of example?



const RO_KEY='keyABCDEF111_YOUR_API KEY';
const newRecords={records:[
    {fields:{"Name": "record5", "Notes": "Test NEW created"} },
    {fields:{"Status": "Todo","Email": "apirest@gmail.com"} }
]}
const options={
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer '+ RO_KEY},
    body: JSON.stringify(newRecords)
}

//Test Api
const TAB_PATH='https://api.airtable.com/v0/appYOURBASEID/tabName';
let word=TAB_PATH//+`view=Grid%20view`;
//let word=TAB_PATH+`filterByFormula=Email%3D%22someaddr%40gmail.com%22`
let response = await remoteFetchAsync(word, options)

const pageSummary = await response.json();
output.inspect(response);
output.inspect(pageSummary); //for debug
//let values=pageSummary.records.map(rec=>rec.fields)
//output.inspect(values)
Matteo_Cossu3
6 - Interface Innovator
6 - Interface Innovator

@Alexey_Gusev this is great!
Does this method work with a batch create? or will i have iterate API calls?

The first argument should be an array of up to 10 record objects. Each of these objects should have one key whose value is an inner object containing your record’s cell values, keyed by either field name or field id.

Returns an array of record objects created if the call succeeded, including record IDs which will uniquely identify the records

Thanks @Alexey_Gusev!

If I copy your syntax it works but whenever I attempt to pass this:

{"0":{"fields":{"Creative_Name":"Audio Produced : PRE+POST – ",
"Class":{"name":"Audio"},"Type":{"name":"Produced"},
"Messaging":{"name":"PRE+POST"}}},

"1":{"fields":{"Creative_Name":"Audio Scripted : PRE+POST – ",
"Class":{"name":"Audio"},
"Type":{"name":"Scripted"},"
Messaging":{"name":"PRE+POST"}}},

"2":{"fields":{"Creative_Name":"Promo 1 : POST – ",
"Class":{"name":"AV"},
"Type":{"name":"Promo"},
"Messaging":{"name":"POST"}}},

"3":{"fields":{"Creative_Name":"Trailer : PRE – ",
"Class":{"name":"AV"},
"Type":{"name":"Trailer"},
"Messaging":{"name":"PRE"}}}}

I get a status: 422 “Unprocessable Entity” … is it the index numbers?

Hi,

it expects array [ ] to process
the thing on your quote is an object with numbered properties, whose values represents required data.
you can’t use Attay.from… or spread operator like […your_object ], because it’s not iterable.
There are special 3 commands for that case: Object.keys() , Object values() and Object.entries()
You need values() to get an array of required type

let passeditem={"0":{"fields":{"......
//i put whole object inside variable`
..}

image

WebHooks in automation would be worth a look, as you won’t need your API key, the webhook provides a unique URL to use. I’ve been testing them out and they seem straight forward enough.

Can’t agree more. I’m using webhooks for inter-base communication, where syncing tables is too redundant for the task. I’ve set up dedicated base as ‘central connection point’ with a list of org structure units and their webhook links and other stuff, a kind of ‘DNS server’ for central management.

Also, it provides convenient UI to start “parse” request body, without all those “array or object” confusions. 100k is a hundreds of records, maybe even 1000+ for narrow tables - no need to iterate for each 10. I just forget this way because never used it for bulk creation, using ‘sync copy’ instead.