Sep 30, 2024 01:59 PM
I am creating an automation script in transferring data from a source table to a target table. The source contains AI generated responses so everything is in text. I have 4 fields in the target table as single-select fields and I found out that the error I am getting is for those fields. Here's the code below. Hope someone can help.
Sep 30, 2024 06:36 PM
Check out the API docs for the expected formatting when updating single select fields: https://airtable.com/developers/scripting/api/cell_values#single-select
It expects:
{ id: string } | { name: string }
Sep 30, 2024 06:58 PM
If I am to correct my script, how would that apply?
Oct 02, 2024 01:20 PM
Hi,
I don't like to edit chatGPT generated code, so I rewrited it using my templates for basic operations
unfortunately, can't test, too much tables/fields to create
complete writeto={ } with other field pairs
const source=base.getTable('Update Repository')
const dest=base.getTable('UPdates')
const {recordId,newRecord}=input.config()
const rec=await source.selectRecordAsync(recordId)
if(!rec) throw new Error('record not exists')
const writeto={
'Future Exit - AI' : 'Future_Exit',
'Future Shutdown - AI' : 'Future_Shutdown',
'One-liner AI' : '1Liner_Up',
'Website Update - AI' : 'Co-URL_Up'
}
const flds=writeto.keys().map(k=>source.getField(k))
const flag=x=>['None','None.'].includes(x)? 'Not mentioned':'Mentioned'
const convert=(fld,val=rec.getCellValue(fld))=>fld.type.includes('elect')
? {name:flag(val)}: writeto[fld.name].includes('URL')
? (val.startsWith('http')? val:null ) : val
const update=Object.fromEntries(flds.map(f=>[writeto[f],convert(f)]).filter(el=>el[1]))
console.log(update); //filter el[0,1]=el[field,value]=>filter null values
await dest.updateRecordsAsync([{id:newRecord,fields:update}])
be aware to use with number fields in source, 0 will be filtered same as null.
Additionally, you can use this small script to get field(s) by part of ID, for example FF6 in your case
const fid=await input.textAsync('field ID or part').then(x=>a=>a.filter(b=>b.id.includes(x)));
output.table(base.tables.filter(n=>fid(n.fields).length).map(t=>[t.name,fid(t.fields).map(f=>f.name).join()]))