Help

Upcoming database upgrades. Airtable functionality will be reduced for ~15 minutes at 06:00 UTC on Feb. 4 / 10:00 pm PT on Feb. 3. Learn more here

Update Current and Create New Record via Script (Modifying date fields)

Topic Labels: Scripting extentions
206 3
cancel
Showing results for 
Search instead for 
Did you mean: 

Good afternoon!

I’ve seen something like this get asked a couple of times, but usually there’s no answer or just a recommendation to check documentation or something to that effect.

I am an absolute JS neophyte, but I’m reasonably computer-literate. My goal is this:

A button-activated script that:

  1. Duplicates the selected record
  2. Copies the contents of the “End Date Minus 7” field of the ORIGINAL record into the “Start Date field” of the NEW record
  3. Copies the contents of the “End Date Minus 7” field of the ORIGINAL record into the “End Date” field of the ORIGINAL record.

Essentially it takes a 15-day shift and splits it into two 8-day shifts that overlap by a day.

It’s a little loopy, but the maneuvers seem like they should be simple enough. How would I approach this?

3 Replies 3

Hi,
it may be simple or quite hard depending on the type of fields in your table, that are different from usual text.
for example,
image

for first 3 fields it’s simple - script can just take the value from field ‘Name’ and puts it to the same field of new record.
but then it meets single-select - an object

image

which you can’t just write to a new record. you should write only an object
{name:'Todo'} (or {id:"sel8......"} , but usually name used)
image

there are also some field types require value transforming.

and the fifth field, formula. it’s non-writable, you should just omit such fields.

I did some procedure when I need to clone a part of my records, at first I thought it should be easy, but then I realized that I could just copy-paste then group by data created, sort and bulk edit some columns like in your case.

anyway, I completed my script (months ago). if I find it, i’ll put it here.

yes, it works. modified it to add your field names and run from button.
just set your table name and run

const table=base.getTable('Table1')
const [START,E7,END]=['Start Date','End Date Minus 7','End Date']
const flds=table.fields.filter(f=>(!f.isComputed)&&(f.name!=START))
const convert=(fieldType,value)=>fieldType=='singleSelect'? {'name':value.name}:
fieldType=='multipleSelects'? value.map(v=>({'name':v.name})):
fieldType=='multipleAttachments'? value.url:
fieldType=='multipleRecordLinks'? value.map(v=>({'id':v.id})): value;

const rec=await input.recordAsync('',table);
const newrow=r=>Object.fromEntries([[START,r.getCellValue(E7)],...flds.map(f=>
[f.name,convert(f.type,r.getCellValue(f))])]);
if(!rec) throw new Error('Record not defined')
await table.createRecordAsync(newrow(rec));
await table.updateRecordAsync(rec.id,{[END]:rec.getCellValue(E7)})

Thank you for the help Alexey!

Right now I’m getting an error when trying to run the script:

ERROR
TypeError: Cannot read properties of null (reading ‘map’)
at convert on line 7
at on line 11
at newrow on line 10
at main on line 13

If it helps, “Start Date” and “End Date” are date fields. “End Date Minus 7” is a formula field that uses the following formula:

DATEADD({End Date Minus 7}, -7, ‘days’)