I am trying to get a script to run within an automation (I have used AI to write this as my scripting knowledge is low) however I can not seem to get it to write to the single select cell called status fix. If anyone has any idea i would be forever greatful!
let table = base.getTable('RSS Feed');
let query = await table.selectRecordsAsync();
// Function to determine the status
function determineStatus(suffix, isFirstTime) {
if (suffix === 1) {
return isFirstTime ? "First Reading" : "Select Committee";
} else if (suffix === 2) {
return isFirstTime ? "Second Reading" : "Committee of the Whole House";
} else if (suffix === 3) {
return "Third Reading";
}
return "Unknown";
}
// Main function to process records
async function processRecords() {
let titleCount = {}; // To count occurrences of each title and base number combination
for (let record of query.records) {
let currentNo = record.getCellValue('No');
let currentTitle = record.getCellValue('Title - RSS');
let currentStatusFix = record.getCellValue('Status Fix');
// Check if currentNo is not null and matches expected pattern
if (currentNo && typeof currentNo === 'string') {
// Check if the No field matches the pattern (1 or more digits followed by -1, -2, or -3)
let match = currentNo.match(/^(\d+)-([123])$/);
if (match) {
let baseNumber = match[1];
let suffix = parseInt(match[2]);
// Create a unique key for tracking occurrences of each title and base number combination
let key = `${currentTitle}-${baseNumber}`;
// Count occurrences of each title and base number combination
titleCount[key] = (titleCount[key] || 0) + 1;
// Determine if it's the first occurrence based on count
let isFirstTime = (titleCount[key] === 1);
// Determine status based on suffix and isFirstTime
let status = determineStatus(suffix, isFirstTime);
// Update Status Fix field if it's different from current value or blank
if (!currentStatusFix || currentStatusFix.name !== status) {
await table.updateRecordAsync(record.id, {
"Status Fix": {name: status}
});
console.log(`Updated Status Fix for record ${record.id}: ${status}`);
}
} else {
console.log(`Record ${record.id}: 'No' field doesn't match expected pattern`);
}
} else {
console.log(`Skipping record ${record.id}: 'No' field is empty or invalid`);
}
}
}
// Run the main function
await processRecords();
console.log("Processing complete");