I got a script to work, am quite pleased with myself and thought I’d share it. I was also guided by some other posts here (in particular this one) though I didn’t fully understand it. I haven’t coded for many years, and never in javascript.
This script runs through a list of job bookings at various properties (for cleaning services in this instance). It is useful for each booking to know when is the next booking for that property. The script runs through all bookings in the scheduling window (limited by view). For each job it finds all other jobs at that property. It runs through all those jobs in order to find the very next one if it exists. If so, it links the current booking to the next booking.
All this is needed because bookings come in in no particular order, and there are often lots of cancellations and adjustments before the job gets done, and because the actally cleaning is often scheduled long after the bookings come in and can be done any time between one job and the next.
Script takes a while to run (maybe because it is a dumb design?) but is worth it.
let table = base.getTable("Jobs");
let view = table.getView("2. Schedule Jobs");
let property = table.getField('Property String');
let start = table.getField('Scheduled Start');
let result = await view.selectRecordsAsync({
sorts: [{field: "Scheduled Start", direction: "asc"}]
});
for (let record1 of result.records) {
await table.updateRecordAsync(record1, {'Next Job': null});
let match = record1.getCellValue(property);
let current = record1.getCellValue(start);
let records2;
records2 = result.records.filter(record => record.getCellValue(property) == match);
let next;
for (let record2 of records2) {
let next = record2.getCellValue(start);
if (next > current) {
await table.updateRecordAsync(record1, {'Next Job': [{id: record2.id}]});
break;}
}
// output.text(match);
}
output.text('Updates Completed')