Help

Converting "Create records for multiple templates" extension to automation script

2112 4
cancel
Showing results for 
Search instead for 
Did you mean: 
Jodi_Harris
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi all! Hoping you can help me with something. 

Context- I need to be able to load a template via an interface (therefore, can't set up a button to do it). 
There is a handy little extension in Airtable called "Create records for multiple templates" that basically sets up a script for you to achieve this. However, because I need to move this into an automation, and the automation action "run a script" can't communicate with the extensions dashboard, I need to convert the dashboard script to work for automations. 

I've updated the //define fields from script settings portion, but I haven't worked out how to hard code in the part where it wants user input on which record to choose. I've pasted the script below, and the lines (I think) that are the issue are 24-34 (under the // Select parent record to create child records & select type section). I basically want it to run the script on the record in which the automation is triggered by. 

So in this case, there is a checkbox that will be checked, and I want the script to run on that record. 

Any help would be greatly appreciated!

Script as follows: 

// Define tables from script settings

let parentTable = base.getTable("LOA Log");

let typesTable = base.getTable("Projects");

let childTable = base.getTable("LOA Tasks");

let templateTable = base.getTable("PROCESS TEMPLATES");

if ([typesTable, childTable, templateTable].indexOf(parentTable) != -1 ||

[childTable, templateTable, parentTable].indexOf(typesTable) != -1 ||

[templateTable, parentTable, typesTable].indexOf(childTable) != -1) {

throw new Error("Parent table, type table, template table, and child table should all be different tables.")

}

// Define fields from script settings

let childFieldInTemplate = templateTable.getField("Task");

let childNameInChild = childTable.getField("Task");

let childOrder = childTable.getField("Task Order");

let templateOrder = templateTable.getField("Task Order");

let templateType = templateTable.getField("Process");

let parentFieldInChild = childTable.getField("Record");

let projectType = parentTable.getField("Project");

// Select parent record to create child records & select type

let selectedEvent = await input.recordAsync('Choose record', parentTable);

while (!selectedEvent) {

output.text('You must select a record.');

selectedEvent = await input.recordAsync('Choose record', parentTable);

}

let parentType = selectedEvent.getCellValue(settings.parentType);

// Look up template records

let typesQuery = await templateTable.selectRecordsAsync();

let typesRecords = typesQuery.records;

// Filter the template records to match the selected type & add the parent ID to the map

let types = typesRecords.map(c => ({

'child': [c],

'childName': c.getCellValue(childFieldInTemplate),

'templateTypes': c.getCellValue(templateType).map(x => x.id),

'templateOrder': c.getCellValue(templateOrder),

// Add additional template fields here and in section below using format below.

// Field names within c.getCellValue parentheticals should match field names in template table

// 'templatePhase':c.getCellValue('Phase'),

// 'templateDays': c.getCellValue('Days')

'templateChecklist': c.getCellValue('Checklist'),

'templateNotes': c.getCellValue('Notes'),

'templateStartDate': c.getCellValue('Start Date'),

'templateDueDate': c.getCellValue('Due Date'),

'templateReference': c.getCellValue('Reference'),

'templateAttachments': c.getCellValue('Attachments'),

'templateAssignee': c.getCellValue('Assignee'),

'templateRole': c.getCellValue('Role Responsible'),

'templateStatus': c.getCellValue('Status'),

})).filter(x => x.templateTypes.includes(parentType[0].id))

// Create the child records and sort them so that they are in order

let createRecords = types.map(c => ({

fields: {

[childNameInChild.name]: c.childName,

[parentFieldInChild.name]: [selectedEvent],

[childOrder.name]: c.templateOrder,

// Add additional template fields here and in section above using format below.

// Field names on the left should match field names in child table.

// Field names on the right following c. should match names created in section above that starts at line 72.

// 'Phase':c.templatePhase,

// 'Days': c.templateDays

'Checklist':c.templateChecklist,

'Notes': c.templateNotes,

'Start Date': c.templateStartDate,

'Due Date': c.templateDueDate,

'Reference': c.templateReference,

'Attachments': c.templateAttachments,

'Assignee': c.templateAssignee,

'Role Responsible': c.templateRole,

'Status': {name: "To Do"},

}

})).sort((a, b) => {

return a.fields[childOrder] - b.fields[childOrder];

});

if (selectedEvent) {

// create records in batches of 50

while (createRecords.length > 0) {

await childTable.createRecordsAsync(createRecords.slice(0, 50));

createRecords = createRecords.slice(50);

}

}

output.text('Done!');
 

 

4 Replies 4

Hi @Jodi_Harris 

You can use the INPUT (left panel while editing the script in the automation) to include the Record id. 

You will need to define that in the script 

 
let config = input.config();

let recordId = config.recordId;
 
Screen Shot 2022-12-20 at 6.35.52 PM.png

 You can then change this part 

let selectedEvent = await input.recordAsync('Choose record', parentTable);

to be 

let selectedEvent = recordId 

Thank you! I feel like I'm very close but something is still off. Here's a screenshot of how I set it up and the error: 

Also here's the code as a whole in case that's helpful: 

// Define tables from script settings

let parentTable = base.getTable("LOA Log");

let typesTable = base.getTable("Projects");

let childTable = base.getTable("LOA Tasks");

let templateTable = base.getTable("PROCESS TEMPLATES");

if ([typesTable, childTable, templateTable].indexOf(parentTable) != -1 ||

[childTable, templateTable, parentTable].indexOf(typesTable) != -1 ||

[templateTable, parentTable, typesTable].indexOf(childTable) != -1) {

throw new Error("Parent table, type table, template table, and child table should all be different tables.")

}

// Define fields from script settings

let childFieldInTemplate = templateTable.getField("Task");

let childNameInChild = childTable.getField("Task");

let childOrder = childTable.getField("Task Order");

let templateOrder = templateTable.getField("Task Order");

let templateType = templateTable.getField("Process");

let parentFieldInChild = childTable.getField("Record");

let parentType = parentTable.getField("Project");

// Define Inpute Variables

let config = input.config();

let recordid = config.recordid;

// Select parent record to create child records & select type

let selectedEvent = recordid;

while (!selectedEvent) {

output.text('You must select a record.');

selectedEvent = await input.recordAsync('Choose record', parentTable);

}

let parentType = selectedEvent.getCellValue(settings.parentType);

// Look up template records

let typesQuery = await templateTable.selectRecordsAsync();

let typesRecords = typesQuery.records;

// Filter the template records to match the selected type & add the parent ID to the map

let types = typesRecords.map(c => ({

'child': [c],

'childName': c.getCellValue(childFieldInTemplate),

'templateTypes': c.getCellValue(templateType).map(x => x.id),

'templateOrder': c.getCellValue(templateOrder),

// Add additional template fields here and in section below using format below.

// Field names within c.getCellValue parentheticals should match field names in template table

// 'templatePhase':c.getCellValue('Phase'),

// 'templateDays': c.getCellValue('Days')

'templateChecklist': c.getCellValue('Checklist'),

'templateNotes': c.getCellValue('Notes'),

'templateStartDate': c.getCellValue('Start Date'),

'templateDueDate': c.getCellValue('Due Date'),

'templateReference': c.getCellValue('Reference'),

'templateAttachments': c.getCellValue('Attachments'),

'templateAssignee': c.getCellValue('Assignee'),

'templateRole': c.getCellValue('Role Responsible'),

'templateStatus': c.getCellValue('Status'),

})).filter(x => x.templateTypes.includes(parentType[0].id))

// Create the child records and sort them so that they are in order

let createRecords = types.map(c => ({

fields: {

[childNameInChild.name]: c.childName,

[parentFieldInChild.name]: [selectedEvent],

[childOrder.name]: c.templateOrder,

// Add additional template fields here and in section above using format below.

// Field names on the left should match field names in child table.

// Field names on the right following c. should match names created in section above that starts at line 72.

// 'Phase':c.templatePhase,

// 'Days': c.templateDays

'Checklist':c.templateChecklist,

'Notes': c.templateNotes,

'Start Date': c.templateStartDate,

'Due Date': c.templateDueDate,

'Reference': c.templateReference,

'Attachments': c.templateAttachments,

'Assignee': c.templateAssignee,

'Role Responsible': c.templateRole,

'Status': {name: "To Do"},

}

})).sort((a, b) => {

return a.fields[childOrder] - b.fields[childOrder];

});

if (selectedEvent) {

// create records in batches of 50

while (createRecords.length > 0) {

await childTable.createRecordsAsync(createRecords.slice(0, 50));

createRecords = createRecords.slice(50);

}

}

output.text('Done!');

 

Hi @Jodi_Harris 

 

Try removing line 32 

Jodi_Harris
5 - Automation Enthusiast
5 - Automation Enthusiast

Got this error after removing line 32

ERROR

TypeError: Cannot read properties of undefined (reading 'id')
at <anonymous> on line 56 at main on line 56