Help

The Airtable Community will undergo scheduled maintenance on September 17 from 10:00 PM PST to 11:15 PM PST. During this period, you may experience temporary disruptions. We apologize for any inconvenience and appreciate your understanding.

Airtable script not writing to the single select column

Topic Labels: Automations Sync
387 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Alex_de_Luen
4 - Data Explorer
4 - Data Explorer

Hi all, 

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");
1 Reply 1

Could you explain what you're trying to do?  Could you provide a read-only invite link to a duplicated copy of your base with some example data as well? 

Once I know what you're trying to do and what your base setup is like I'll try to help!