Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Jul 06, 2019 05:59 PM
I’m trying to insert a new record into a table that has a link field (single select). The value I’m trying to insert matches exactly with the value in the linked table but I’m getting a 422 error.
To do this do I need to insert the record ID in the linked table? I don’t have this ID available in my external application.
Jul 13, 2019 04:40 PM
If you are trying to modify a linked record field (or create a new record with a linked record field), you do need to use the foreign record’s ID. If you don’t have those IDs readily available, you can look them up via a GET request and use the filterByFormula
query parameter to find the foreign record ID. For example, if you’re foreign record’s primary key is called “Name” with a value “Record1”, you could find the record by doing:
curl -X GET 'https://api.airtable.com/v0/YOUR_BASE_ID/YOUR_FOREIGN_TABLE_NAME?filterByFormula={Name}=%22Record1%22'
This will return a list of records that match that formula. If your primary keys are unique, there will only be one record in the list and you can retrieve it’s record ID.
You could also try and use the typecast
parameter when creating the new record. Using typecast
will ask the Airtable API to try and infer the intention of the request. You can use the foreign record’s primary key instead of it’s record ID, and the API will attempt to find the corresponding linked record and make the connection:
curl -X POST \
https://api.airtable.com/v0/YOUR_BASE_ID/YOUR_TABLE_NAME \
-H 'Content-Type: application/json' \
-d '{
"fields": {
"Linked Field": ["Record1"]
},
"typecast": true
}'
Jul 14, 2019 10:31 AM
Thanks! I got this working using the typecast parameter.
Apr 02, 2020 01:59 PM
I am trying to create a new record with a linked record field.
I followed your instructions and am setting the linked field to the foreign record ID (recXORCOwTG4hhDBF), but I am still getting a 422 error.
I am able to create the new record without the linked field, so it seems like there is something wrong with the linked field.
Apr 02, 2020 02:45 PM
Hi @Richard_Lo1
Are you passing that record ID in inside of an array, or just as a string value for the Linked field?
I’m fairly certain that you have to pass an array of string ID values to a Linked field, even if you are only trying to insert 1.
So instead of:
"fields": {
"Linked Field": "recXORCOwTG4hhDBF"
}
You need:
"fields": {
"Linked Field": ["recXORCOwTG4hhDBF"]
}
Apr 02, 2020 03:04 PM
Hey Jeremy_Oglesby,
You’re right! Passing the record ID inside an array worked!
I was trying to pass the string value and was getting an error. Thanks for the quick help!
Jul 25, 2020 03:24 PM
Encountered this as well and for those that it might help, the typecast parameter only works for string values. So the primary field you are linking to must be a text field. I tried this with a number field as the primary field and it kept creating new records even though one already existed.
Feb 26, 2022 11:18 AM
How can I use this in Laravel? Can I use something like this?
$ids = [
“Company_tags” => [$id]
];
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => env('AIRTABLE_API_KEY')
])->post('https://api.airtable.com/v0/'.env('AIRTABLE_BASE_ID').'/'.env('AIRTABLE_CAND_TABLE'), [
"typecast" => true,
"fields" => [
"Company_tags" => [$id],
],
]);
Feb 26, 2022 12:06 PM
How can I it in Laravel? Can I use it something like this?
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => env('AIRTABLE_API_KEY')
])->post('https://api.airtable.com/v0/'.env('AIRTABLE_BASE_ID').'/'.env('AIRTABLE_CAND_TABLE'), [
"typecast" => true,
"fields" => [ "Company_tags" => [$id] ]
]);
Sep 19, 2022 06:43 PM
“Linked Field”: [“Record1”]
If i want to have 3 records inside “Linked Field”, what should i type?