Help

Welcome to the Airtable Community! If you're new here, check out our Getting Started area to get the most out of your community experience.

Re: Duplicating a record, then duplicating the linked records

163 0
cancel
Showing results for 
Search instead for 
Did you mean: 

Greetings!

I’m working on an airtable automation to operate from an interface button. the automation would do the following (the part im having trouble with is the script as the last step):
Screen Shot 2022-09-28 at 10.49.06 AM

from the rest of the automation im passing three values into the script as inputs:

  • The original project record ID
  • The duplicated Project Record ID
  • a list of tasks that are linked to the original project (this one im not using for the script, but assume it would be more efficient to use this rather than query the entire list of 1000+ tasks.

the script is currently “functional” but I’d like to make the following changes and im running up against my JS ability.

  • use the list of IDs to make the script more efficient instead of the query
  • when the fields are being set in that For loop, set the Project field to the new duplicated project as part of that same step.
  • set another field called “Status” to the string “Planned”, otherwise all fields can simply be duplicates.

Any help would be much appreciated!

let inputConfig = input.config();
console.log(`The value of projectRecord is ${inputConfig.projectRecord}`);

let projectRecord = inputConfig.projectRecord;
let newProject = inputConfig.newProject;
console.log(`The value of new project is ${newProject}`);
let oldTasks = inputConfig.Tasks;
console.log(`The value of old task IDs is ${oldTasks}`);

let table = base.getTable('Tasks');
let queryResult = await table.selectRecordsAsync();
let checked = queryResult.records.filter(record => {return record.getCellValue('projRecord') == projectRecord})
// let checked = queryResult.getRecord(queryResult.recordIds[0]);
// Make new records based on those that were checked

let newRecords = []
for (let record of checked) {
    let newRecord = {fields: {}}
    for (let field of table.fields) {
        // @ts-ignore
        if (field.isComputed || field.name === "Copy")
            continue
        let value = record.getCellValue(field.name)
        if (value === record.name)
            value += " copy"
        newRecord.fields[field.name] = value;
    }
    newRecords.push(newRecord);
    table.updateRecordAsync(record, {'Project': newProject})
}

while (newRecords.length > 0) {
    await table.createRecordsAsync(newRecords.slice(0, 50))
    newRecords = newRecords.slice(50)
output.set('tasknew', newRecords);
}
4 Replies 4

Hi,

that’s cool, I did some scripts where fields looped, but never used isComputed property, how can I forget…

i can’t check and i feel it may be bugged somewhere, but i see it that way (suppose primary field is ‘Name’):

let (projectRecord,newProject,oldTasks} = input.config();
let table = base.getTable('Tasks');
let goodfields=table.fields.filter(f=>!f.isComputed&&f.name!='Prio')
let query = await table.selectRecordsAsync({fields:goodfields, recordIds:oldTasks});
let duplicate=({'Name':n,'Project':p,'Status':s,...others}) => ({'Name':n+' copy','Project':newProject,'Status':'Planned',...others})
let oldRecs = query.records.map(r=>Object.fromEntries(goodfields.map(gf=>[gf.name,r.getCellValue(gf)])))
let newRecs = oldRecs.map(rec=>({fields:duplicate(rec)))
while (newRecs.length) await table.createRecordsAsync(newRecs.splice(0,50))

Thanks for the response! I’ll try this

So I’ve tested it, and im getting a similar error as before, and I did modify your script a bit. (the error refers to line 8 which is the last line of the script but I am assuming it’s referring to the Project field.

let {projectRecord,newProject,oldTasks} = input.config();
let table = base.getTable('Tasks');
let goodfields=table.fields.filter(f=>!f.isComputed&&f.name!='Prio')
let query = await table.selectRecordsAsync({fields:goodfields, recordIds:oldTasks});
let duplicate=({'Name':n,'Project':p,'Status':s,...others}) => ({'Name':n+' copy','Project':newProject,'Status':'Planned',...others})
let oldRecs = query.records.map(r=>Object.fromEntries(goodfields.map(gf=>[gf.name,r.getCellValue(gf)])))
let newRecs = oldRecs.map(rec=>({fields:duplicate(rec)}))
while (newRecs.length) await table.createRecordsAsync(newRecs.splice(0,50))

Screen Shot 2022-09-30 at 11.24.23 AM

You have field id, so you can check whether it’s ‘Project’. Use Manage Fields or output.table(Object.fromEntries([...base.getTable(cursor.activeTableId||'').fields.map(x=>([x.name,{type:x.type,id:x.id}]))]))

i suppose your Project field is not text but maybe multipleLinks. it needs array of links, so use:

let duplicate=({'Name':n,'Project':p,'Status':s,...others}) => ({'Name':n+' copy', Project’: [ {‘id’:newProject} ] ,'Status':'Planned',...others})