I have been trying to get the script "Create records for multiple templates" to work in an automation script. I am trying to get this to work because the button I have in the base the run the extension script does not work in Interface Designer. Any Help or insight to get this to work would be greatly appreciated, or if you know of a work around to get the button to work in the interface that would work as well.
Below is the original script and the values from the base used to the script prompt.
// Create settings
let settings = input.config({
title: 'Create records for multiple templates',
description: 'This script will create records in a child table that link back to a parent record and are based on the template records that correspond to the parent record type',
items: [
input.config.table('parentTable', {
label: 'Parent table',
description: 'Table from which you need to create template records (ex: Projects; Campaigns)'
}),
input.config.table('typeTable', {
label: 'Type table',
description: 'Reference table with records for each template type (ex: Project categories; Campaign sizes)'
}),
input.config.table('templateTable', {
label: 'Template table',
description: 'Reference table with records for each template record and a linked record to the Type Table (ex: Task templates; Activity templates)'
}),
input.config.table('childTable', {
label: 'Child table',
description: 'Table in which you need to create records based on the type linked record (ex: Tasks; Activities)'
}),
input.config.field('parentType', {
parentTable: 'parentTable',
label: 'Parent type',
description: 'Linked record in parent table to type table (ex: Project type; Campaign size)'
}),
input.config.field('templateType', {
parentTable: 'templateTable',
label: 'Template type',
description: 'Linked record in template table to Type Table (ex: Project type; Campaign size)'
}),
input.config.field('childFieldInTemplate', {
parentTable: 'templateTable',
label: 'Child field in template table',
description: 'Text field in template table to indicate name of record (ex: Task name; Activity name)'
}),
input.config.field('templateOrder', {
parentTable: 'templateTable',
label: 'Template record order',
description: 'Number field in template table to indicate order in which records should be executed (ex: Task order; Activity order)'
}),
input.config.field('childOrder', {
parentTable: 'childTable',
label: 'Child record order',
description: 'Number field in child table to indicate order in which records should be executed (ex: Task order; Activity order)'
}),
input.config.field('childNameInChild', {
parentTable: 'childTable',
label: 'Child name',
description: 'Text field in child table to indicate name of record (ex: Task name; Activity name)'
}),
input.config.field('parentFieldInChild', {
parentTable: 'childTable',
label: 'Parent field in child table',
description: 'Linked record in child table to indicate related parent record (ex: Project; Campaign)'
})
],
})
// Define tables from script settings
let parentTable = settings.parentTable;
let typesTable = settings.typeTable;
let childTable = settings.childTable;
let templateTable = settings.templateTable;
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 = settings.childFieldInTemplate;
let childNameInChild = settings.childNameInChild;
let childOrder = settings.childOrder;
let templateOrder = settings.templateOrder;
let templateType = settings.templateType;
let parentFieldInChild = settings.parentFieldInChild;
// 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')
})).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
}
})).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!');