Welcome to lesson 7 of my Airtable scripting series. In this lesson, I will go through how to Take User Input in your Airtable Script. There are two ways to take user input:
I will explain both ways in this lesson. First, let’s understand the direct way Firstly, you need to install the scripting app, then you need to copy and paste the code below into your app. For this lesson, I am using the previous lesson’s table and code. You can click here to read the previous lesson.
/* -------------- Selecting the table & Getting selected record data -------------- */
let table = base.getTable('Currencies'); let record = await input.recordAsync('Select a record to use', table);
/* -------------- Getting data from API -------------- */
let apiResponse = await fetch('https://api.exchangerate.host/latest?base=USD');
/* -------------- USING API DATA -------------- */
let data = await apiResponse.json(); let conversionRateINR = data.rates.INR; let conversionRateEURO = data.rates.EUR;
/* -------------- Updating data -------------- */
await table.updateRecordAsync(record.id, { 'In INR': record.getCellValue('AMOUNT IN USD') * conversionRateINR, 'IN EURO': record.getCellValue('AMOUNT IN USD') * conversionRateEURO, });
If you are coming from the last article you might already have noticed that the code is exactly the same, with the exception of the second line. That is the only line of code you need to learn in this lesson. Here we are using 'input.recordAsync' which is used to take user input. We are passing two arguments:
Now you can run the script and once you run it, it will ask you to select an existing record from the table. Once you select the record, the script will do the remaining job. Let me explain the above code in steps:
Now, what if you want to take user’s input from a button click?
It’s simple, just create a new column in the table and choose the button as a type of column. In the button configuration, you just need to select 'run a script' as an action. The script will automatically take the clicked button’s record as user input and use it in the process. The image below shows how to configure the button action.
Stay connected for more learning lessons.
Feel free to contact me for Airtable consulting services by CLICKING HERE.