Skip to main content
Solved

Create multiple records based on linked cell values

  • November 1, 2020
  • 1 reply
  • 26 views

Forum|alt.badge.img+6

I have a script that’s currently working to create one record on my Project/Yarn junction table, but I need it to create multiple records, one per linked cell value in Yarn (from stash), if there are multiple of these. I’ve had a looked on the forums but am struggling to find the solution, can anyone help? This is my current script:

let table = base.getTable(“Projects”);
let record = await input.recordAsync(‘Select a record to use’, table);
let populateTable = base.getTable(‘Project/Yarn’);
if (record) {
await populateTable.createRecordAsync({
“Project”: [{id: record.id}],
“Yarn used”: record.getCellValue(‘Yarn (from stash)’),
})
output.text(Record created: ${record.name})
} else {
output.text(‘No record was selected’);
}

Best answer by Nicole_Hawkesf1

Solved it:

// Change this name to use a different table
let table = base.getTable(“Projects”);
// Prompt the user to pick a record
// If this script is run from a button field, this will use the button’s record instead.
let record = await input.recordAsync(‘Select a record to use’, table);
let populateTable = base.getTable(‘Project/Yarn’);
let linkedRecordCellValue = record.getCellValue(‘Yarn (from stash)’);
for (let yarn of linkedRecordCellValue) {
// The below creates a record on the Project/Yarn table and populates
// the Project and Yarn fields with the data from the Project record
await populateTable.createRecordAsync({
“Project”: [{id: record.id}],
“Yarn used”: [{id: yarn.id}],
})
output.text(Records created: ${record.name}, ${yarn.name})
}

1 reply

Forum|alt.badge.img+6
  • Author
  • Inspiring
  • 8 replies
  • Answer
  • November 1, 2020

Solved it:

// Change this name to use a different table
let table = base.getTable(“Projects”);
// Prompt the user to pick a record
// If this script is run from a button field, this will use the button’s record instead.
let record = await input.recordAsync(‘Select a record to use’, table);
let populateTable = base.getTable(‘Project/Yarn’);
let linkedRecordCellValue = record.getCellValue(‘Yarn (from stash)’);
for (let yarn of linkedRecordCellValue) {
// The below creates a record on the Project/Yarn table and populates
// the Project and Yarn fields with the data from the Project record
await populateTable.createRecordAsync({
“Project”: [{id: record.id}],
“Yarn used”: [{id: yarn.id}],
})
output.text(Records created: ${record.name}, ${yarn.name})
}