Jun 20, 2020 09:52 PM
Does typecast only work on .create? What if I want to update a mule-select field in update?
base('Sites').update([ 79 { 1 "id": recId, 2 "fields": { 3 "siteStreetAddress":
{!!placeDetailsObj.street_number ? placeDetailsObj.street_number + ' ' : ''}{!!placeDetailsObj.route ? placeDetailsObj.route : ‘’}, 4 "siteCounty":
{!!placeDetailsObj.administrative_area_level_2 ? placeDetailsObj.administrative_area_level_2 : ''}`,
5 "siteState": `{!!placeDetailsObj.administrative_area_level_1 ? placeDetailsObj.administrative_area_level_1 : ‘’}, 6 "siteCity":
{!!placeDetailsObj.locality ? placeDetailsObj.locality : ''}`,
7 "siteZipExt": `{!!placeDetailsObj.postal_code_suffix ? placeDetailsObj.postal_code_suffix : ‘’}, 8 "siteZip":
{!!placeDetailsObj.postal_code ? placeDetailsObj.postal_code : ''}`,
9 "siteNeighborhood": `{!!placeDetailsObj.neighborhood ? placeDetailsObj.neighborhood : ‘’}, 10 "siteGmapsUrl":
${!!placeDetailsObj.mapsUrl ? placeDetailsObj.mapsUrl : ‘’}, 11 "lat": placeDetailsObj.location.lat, 12 "lng": placeDetailsObj.location.lng, 13 "allGmapsTypes": placeDetailsObj.types 14 } 15 } 16 ], {typecast: true}, (err, record) => { 17 18 if (err) { 19 console.error(err); 20 return; 21 } 22 23 console.log(record.get('siteName')); 24 });
Jul 16, 2020 05:12 PM
Hi, @Lean_Junio,
Yes, typecast
also works when updating records.
Airtable’s API documentation for the “update” command states:
Automatic data conversion for update actions can be enabled via typecast parameter. See create record for details.
I verified this using the airtable.js library. Here’s my Node.js script:
'use strict';
var Airtable = require('airtable');
var apiKey = 'replace this string with an authentic API key';
var baseID = 'replace this string with an authentic Base ID';
var recordID = 'replace this string with an authentic record ID';
var base = new Airtable({apiKey: apiKey}).base(baseID);
base('SMS').update([
{
id: recordID,
fields: {
'my text field': '45'
}
}
], {typecast: false}, function(err, records) {
if (err) {
console.error(err);
return;
}
console.log('Success!');
});
(By the way: you can improve the formatting of code blocks here on the forum by indenting the code by four spaces.)
I replaced the strings which start “replace this value” with authentic values and ran the script. The code reached the console.error
statement and printed the following error:
AirtableError {
error: 'INVALID_VALUE_FOR_COLUMN',
message: 'Field "my text field" cannot accept the provided value',
statusCode: 422
}
That’s because in my Base, the field named “my text field” is a text field. Airtable rejected my attempt to set it to a string value (even though the string describes a number).
But once I enabled type coercion by changing {typecast: false}
to {typecast: true}
, then the script reached the console.log
statement and printed:
Success!
…and the value of the targeted cell was updated to the number 45 (because Airtable coerced the string value into a number value).