Hi - I’m not great with scripting but trying to learn. All I want to do is copy a set of data - really just one column, from tableA to tableB, monthly via automation. The column is a linked field in the destination table and so would need to be an array of recordIds when passed. I’m not sure how to cleanly do that and am not familiar enough with scripting to fix it. The below are things that I’ve borrowed or taken from elsewhere & tried to adapt, but I’m not quite there. Can someone point me to my issues? I know for example that I am passing only the first element with “t0]” but I don’t know quite how to cycle through all the elements cleanly.
I welcome any code cleanup or suggestions.
let sourcetable = base.getTable("Projects");
let sourceview = sourcetable.getView("ProjectView1");
let destination_table_name = "DestinationTableName";
let destination_table = base.getTable(destination_table_name);
let query = await sourceview.selectRecordsAsync({fields: s"Project Name"]});
let records = query.records;
let recordsToCreate = new Array;
//I think I begin to have trouble here, and I don't know how to properly loop through and push the query results into the array.
for (let projids of query.recordIds){
recordsToCreate.push({
fields:{
"Project" : {id: query.recordIdsu0]}]
}
})
}
//is this right? I think I am probably messing this up too
recordsToCreate = query.records.map(record => record.id);
let recordsCreated = await batchAnd ('Create',destination_table,recordsToCreate);
//this is a function I borrowed from others here on the forums for batch updating >50
// ********************
async function batchAnd(action, table, records) {
let recordsActedOn = records.length;
switch (action) {
case 'Update':
while (records.length > 0) {
await table.updateRecordsAsync(records.slice(0, 50));
records = records.slice(50);
};
break;
case 'Create':
while (records.length > 0) {
await table.createRecordsAsync(records.slice(0, 50));
records = records.slice(50);
};
break;
case 'Delete':
while (records.length > 0) {
await table.deleteRecordsAsync(records.slice(0, 50));
records = records.slice(50);
}
break;
default:
output.markdown(`**Please use either 'Update', 'Create', or 'Delete' as the "action" parameter for the "batchAnd()" function.**`);
recordsActedOn = null;
}
return recordsActedOn;
}