Help

Re: A script for randomly reviewing records and choosing a select field value for each

4106 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Alex_Wolfe1
Airtable Employee
Airtable Employee

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 :slightly_smiling_face:

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

// 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 **`);
6 Replies 6
cesar_nates
4 - Data Explorer
4 - Data Explorer

It doesnt work, i copied the base.
I want to get a random record from a specific Row, and its ID

Hi Cesar - I just copied the base and it worked for me, could you share some details on what’s not working right or a screenshot of what you’re seeing? Happy to help make sure the script is working properly!

Captura de pantalla 2020-10-20 a las 11.29.09 a.m.

Thanks Cesar - Sorry about that! It looks like you’re not in the Script settings beta so the Scripting app isn’t recognizing the input.config() call. If you fill out this form, we can enable it for your account though, and after that the script should work. Let me know if you run into any trouble!

cesar_nates
4 - Data Explorer
4 - Data Explorer

Hi @Alex.Wolfe, is it possible to call a webhook from a button but if one record has value"A", call webhook “1” but if the record has “B” value call webhook “2”? in the same button?

Hey Cesar! If you’re using a button field then you could build in logic using an IF() or similar function (airtable.com/formulas for more on that!) to output one URL under certain conditions, or another URL under other conditions.

So that could look something like the example formula below:

IF({field}="A","url-output-1",IF({field}="B","url-output-2"))