When a record has multiple date fields, sometimes you’d like to know which date is the earliest.
In the below example, each record is a social post that has different live dates for different channels. I’d like to know the earliest live date across all three channels. I could do this in a formula, but the ‘LIVE DATE’ field is also used by other teams, so I’d rather calculate the date with the below script (plus the awesome batchAnd script to do this for more than 50 records):
1 // 🌸🌸 👇Add all the date fields you need here 👇 🌸🌸2let fields = ['Instagram Date','IG Stories Date','Facebook Date','LIVE DATE']34// Let Airtable know which table and views records you want to use5let table = base.getTable("Content");6let view = table.getView('All');7let query = await view.selectRecordsAsync({fields:fields});8let records = query.records;910// Find the Earliest Date11let dates = records.map( c => fields.map( x => (c.getCellValue(x) != null && x != 'LIVE DATE') ? Date.parse(c.getCellValue(x)) : null).filter(x => x));12let minRaw = dates.map( c => c.reduce((acc,cur) => Math.min(acc,cur)));13let min = minRaw.map( c => new Date(c).toISOString())14let update = min.map( (c,i) => ({id:records[i].id,fields:{15 'LIVE DATE': c16}}));1718/*19 Use this function to perform 'Update', 'Create', or 'Delete'20 async actions on batches of records that could potentially 21 more than 50 records.2223 ::PARAMETERS::24 action = string; one of 3 values:25 - 'Update' to call table.updateRecordsAsync()26 - 'Create' to call table.createRecordsAsync()27 - 'Delete' to call table.deleteRecordsAsync()2829 table = Table; the table the action will be performed in3031 records = Array; the records to perform the action on32 - Ensure the record objects inside the array are33 formatted properly for the action you wish to34 perform3536 ::RETURNS::37 recordsActedOn = integer, array of recordId's, or null; 38 - Update Success: integer; the number of records processed by the function39 - Delete Success: integer; the number of records processed by the function40 - Create Success: array; the id strings of records created by the function41 - Failure: null;42*/43async function batchAnd(action, table, records) {44 let recordsActedOn;4546 switch (action) {47 case 'Update':48 recordsActedOn = records.length;49 while (records.length > 0) {50 await table.updateRecordsAsync(records.slice(0, 50));51 records = records.slice(50);52 };53 break;54 55 case 'Create':56 recordsActedOn = [];57 while (records.length > 0) {58 let recordIds = await table.createRecordsAsync(records.slice(0, 50));59 recordsActedOn.push(...recordIds)60 records = records.slice(50);61 };62 break;6364 case 'Delete':65 recordsActedOn = records.length;66 while (records.length > 0) {67 await table.deleteRecordsAsync(records.slice(0, 50));68 records = records.slice(50);69 }70 break;7172 default:73 output.markdown(`**Please use either 'Update', 'Create', or 'Delete' as the "action" parameter for the "batchAnd()" function.**`);74 recordsActedOn = null;75 }76 return recordsActedOn;77}787980// Update the records81await batchAnd('Update',table,update)