BuiltOnAir

Review Random Records

Created by: Creator: Alex Wolfe

Wanted to share this script in case it’s helpful for anyone else with a similar need. I initially came up with the idea so I could randomly review a list of records and assign single-select field values using buttons for each one. This is one of the first scripts I started with, and have come back to it with improvements as I’ve learned — thanks to the incredible help from some teammates :raised_hands:! This version’s been edited for a general audience so that it’s hopefully adaptable to other bases easily :slight_smile:

Note, it uses the Script settings beta 1, and to see what it looks like in a base, here’s an example in Airtable Universe 2.

Script Code


							// SCRIPT SETTINGS
   let settings = input.config({
       title: '👓 Random record reviewer',
       description: 'Pulls a random record, then prompts you to choose a select field option.',
       items: [
           input.config.table('table',{
               label: 'Table'
           }),
           input.config.field('field1',{
               label: 'Select field to EDIT ✍️',
               description: 'A select field type you wish to edit',
               parentTable: 'table'
           }),
           input.config.field('field2',{
               label: 'Primary field to review 👓',
               description: 'Select a field with values to display during the review',
               parentTable: 'table'
           }),
           input.config.field('field3',{
               label: 'Secondary field to review 👓',
               description: 'Select a field with values to display during the review',
               parentTable: 'table'
           }),
           input.config.view('view',{
               label: 'View with records to review 👓',
               description: 'Note: Filter this view based on the Select field (to show only records with empty values) if you\'d like',
               parentTable: 'table'
               })
       ]
   });

   // SETTINGS TO VARIABLES
   let table = settings.table;
   let viewName = settings.view.name
   let view = settings.view;
   let field1 = settings.field1;
   let field2 = settings.field2;
   let field3 = settings.field3;

   // GATHERS RECORDS FOR RANDOM SELECTION
   let listRecords = [];
   let result = await view.selectRecordsAsync();
   for (let record of result.records) 
       {
       listRecords.push(
                       {
                           title: record.getCellValueAsString(field2),
                           notes: record.getCellValueAsString(field3),
                           type: record.getCellValueAsString(field1),
                           id: record.id,
                       }
                       );
       };

   while (listRecords.length > 0) {

       // RANDOMIZER
       let randomIndex = Math.floor(Math.random()*listRecords.length);
       let randomElement = listRecords[randomIndex];

       output.markdown('### Primary field:');
       output.markdown(`${randomElement.title}`);

       output.markdown('### Secondary field:');
       output.markdown(`${randomElement.notes}`);

       output.markdown('******');
       output.markdown('### Select the `'+`${field1.name}`+'` for this record:');

       const optionButtons = async(label, optionValue) => {

           let options = field1.options.choices
           let buttons = []

           for(let o=0; o<options.length; o++){

               let optionName = options[o]['name']

               buttons.push({
                   label: optionName
               })
           }
           
           return input.buttonsAsync('',buttons)
       }

       let optionChosen = await optionButtons('',field1.name);
       let updateRecord = await table.updateRecordAsync(randomElement.id,{
           [`${field1.name}`]: {name: optionChosen},
       })

       // REMOVE THE RECORD FROM THE ARRAY
       listRecords.splice(randomIndex, 1);

       // CLEAR THE OUTPUT FOR THE NEXT RECORD
       output.clear();
       
   }

   output.markdown(`**All done! There are no more records to classify in this view **`);