Help

Re: Modify a script to run on record creation

603 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Alan_Reid
6 - Interface Innovator
6 - Interface Innovator

I have a script that takes the records in a table called "Runs" and creates a separate entry in a table called "Breakout" for each linked entry in the "Runners" field.  However, this script is currently only able to run manually, and if it's run multiple times, it creates duplicate entries.

I'm looking to modify this script so it either runs automatically each time a record is inserted, or when it's run manually, it doesn't create duplicate entries.

Here is the script:

const sourcetable = base.getTable('Runs');
const desttable = base.getTable('Breakout');
const query=await sourcetable.selectRecordsAsync({fields:['Game','Runners']});
const converse=rec=>rec.getCellValue('Runners').map(x=>[rec.getCellValue('Game'),{id:x.id}])
const create=(el)=>({fields:{'Run':el[0], 'Participant':[el[1]]}})
const crt=query.records.flatMap(converse).map(create)
while (crt.length) await desttable.createRecordsAsync(crt.splice(0,50))

 Any assistance would be appreciated!  Thanks

3 Replies 3

What is your coding background and who wrote the script you posted?

Due to the writing style, it is a little hard to follow the logic in your script on a quick read, but I don't think that is causing your problems.

The script currently runs for every record in the table. You have a few options. You can modify the script so that it only runs on a triggering record. Note that if you use an automation script, determining when to trigger the automation can get a little tricky because when the human thinks "record creation" happens doesn't always match when the computer thinks record creation happens.

Another option is to use a filtered view that only shows records that have not been processed. Then run the script only on records in that view.

Another option is to have the script determine which records have been processed, and to filter out records that have been processed. (This is your original request.) However, of the different options, this one requires the most technical coding skills and a solid understanding of the related aspects of the base schema.

I ran into the script Here and it definitely does what I want it to do if only run once after all the data is there.  I'm looking to modify it so it doesn't run on every record on the table, but instead on the one that was just created (via form submission)

Alan_Reid
6 - Interface Innovator
6 - Interface Innovator

Maybe I should rephrase the question.  I'm looking for a script that runs on record creation that takes a field that will have at least one, but possibly more linked records, and writes them out to another table individually.

The final output would look like this

Table 1
Person 1: Events A,B,C
Person 2: Events B,D
Person 3: Event C

 

Table 2:

Person 1: Event A
Person 1: Event B
Person 1: Event C
Person 2: Event B
Person 2: Event D
Person 3: Event C