I have a table of subtasks called subtasks. this is the todo list for the whole team.
I used self-linking to get predecessors setup, and so I have:
- a rollup field showing the predecessor end date (PredEnd)
- a manual task start date (START)
- a manual task duration in days (LT)
- a formulated task completion date that adds the task duration in workdays to the start date (END)
What I want to do now is create a scripting block that can load all the records in this table only, and compare each records PredEnd vs START, and if they are different, to update the START using the correct PredEnd value.
But I keep getting errors and this is my first script… no idea what im doing…
let table = base.getTable('subtasks');
let field = table.getField("START")
let predEnd = table.getField("PredEnd");
let taskStart = table.getField("START");
// Load all of the records in the table
let result = await table.selectRecordsAsync();
// Find every record we need to update
let replacements = [];
for (let record of result.records) {
let startValue = record.getCellValue(taskStart);
let predValue = record.getCellValue(predEnd);
if (predValue !== startValue) {
replacements.push({
record,
before: startValue,
after: predValue,
});
}
}
if (!replacements.length) {
output.text('No replacements found');
} else {
output.markdown('## Replacements');
output.table(replacements);
let updates = replacements.map(replacement => ({
id: replacement.record.id,
field: {
[field.id]: replacement.before,
}
}));
// Only up to 50 updates are allowed at one time, so do it in batches
while (updates.length > 0) {
await table.updateRecordsAsync(updates.slice(0, 50));
updates = updates.slice(50);
}
}