Help

Re: Duplicating Records via Automation Script

990 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Teagan_Clark
5 - Automation Enthusiast
5 - Automation Enthusiast

Javascript baby here. 

My ultimate goal is that when a record is created and matches conditions, a script will run that will duplicate that record a number of times and update it's status. I've been modifying a duplication script from an extension script, but I'm a bit stuck on how to make it work for an automation, and if I'm even doing the right thing. If you have a moment to take a look, I would really appreciate it!

Here is the code as it stands:

let table = base.getTable("🏁 Tasks")
let fields = table.fields
let query = await table.selectRecordsAsync({fields: table.fields})
let records = query.records
//I have a set number of records that will need to be duplicated based on information from the base which I think should be inputConfig.numRecords
let inputConfig = input.config();

//exluding only one field
let exclude = ["Dependent On"]

let filteredFields = fields.filter(x => !exclude.includes(x.name) && x.isComputed == false)

//I'm not sure if I need this part if it's being triggered by an automation instead of an extension
let original = await input.recordAsync("Original Record", table)

//Here is my attempt to have the script create multiple duplicated records based on a number from the base
for (let i = 0; i < inputConfig.numRecords; i++) {
let obj = {};

filteredFields.map(x => {
    Object.assign(obj, {[x.name]: original.getCellValue(x.name)})
})

//I want it to set the field "Reoccuring Task" to "Duplicated", not sure if this is right
obj["Reoccurring Task?"] = "Duplicated";

await table.createRecordAsync(obj)

In the past, I've used make (integromat) to solve for this sort of thing, and I'm sure it could do so very easily, but I'm trying to learn and grow 😊 

3 Replies 3
Jpatton
5 - Automation Enthusiast
5 - Automation Enthusiast

May I ask what you're using this script for? Based on the use case, you might be able to get around using JavaScript entirely.

Definitely happy to look at other options! Essentially the workflow is this:

  • We have projects where we're recording and editing multiple videos.
  • The project manager creates tasks from a for the project using a template extension.
  • Some of the tasks need to occur multiple times, depending on how many videos are being created per project.

The goal: When new tasks are created for a project, I want the automation to check if the task is reoccurring and check how many videos are in the project, and then based on the information, duplicate the task X amount of times. (i.e. We have a project for 3 videos to be recorded and edited, the task "Schedule Recording" is duplicated 2 times for a total of three tasks called "Schedule Recording")

Based on what you've said so far, it sounds like this should be doable via Airtable's built-in automations. The solution I thought of has three basic steps:

1. Create a checkbox filed titled something like "Per Video" in the Task Templates table (I'm assuming you're storing task templates in a table).

2. Trigger copying of task template (I'm assuming there's a button in the Task Templates table that is clicked to initiate the usage of a task template.

3. Run an Airtable automation whose trigger is the click of the aforementioned button that checks whether the "Per Video" checkbox of a task template is populated.

    a. If yes, then for each video linked to the targeted project (I'm assuming you already have a means of indicating which project a task created from a template should be linked to):

        i. Create task from corresponding task template.

        ii. Link the task created to the corresponding video.

        iii. Link the task created to the targeted project.

    b. If no, then:

        i. Create task from corresponding task template.

        ii. Link task to targeted project.