Help

How can I set a checkbox to true, then back to false after a time delay?

Topic Labels: Extensions
Solved
Jump to Solution
2769 6
cancel
Showing results for 
Search instead for 
Did you mean: 
Sean_Patterson1
4 - Data Explorer
4 - Data Explorer

I’m trying to create a script in Airtable linked to a button. The process is as follows:

  1. Button within Airtable pressed
  2. If the checkbox field ‘switch’ is false, then turn it to true
  3. Wait for 2 seconds, reset the field to false

This is the script I’ve written using the setTimeout() function:

let table = base.getTable("EXM"); //The name of the table you're in here
let record = await input.recordAsync('Pick a record', table);
if (record) {
   
 if (record.getCellValue("switch")===false) {
    table.updateRecordAsync(record, {'switch': true});
    output.text('checkbox ticked');
 } 
 
const myTimeout = setTimeout(timeDelay, 2000);
   function timeDelay()
      table.updateRecordAsync(record, {'switch': false});
      output.text('2 second interval');
    
}

When I write the code I get an error that says “cannot find name setTimeout”. As a solution, it suggests “add missing function declaration ‘setTimeout’”

How do I declare this function so it can function with my code?

1 Solution

Accepted Solutions
Zack_S
8 - Airtable Astronomer
8 - Airtable Astronomer

Hi @Sean_Patterson1,

What’s the purpose for your delay? I understand setting delays can sometimes cause other issues.

However, this may work for you.
Also, notice how this line if (record.getCellValue("switch") == null) is equal to null and not false.

function delay(ms) {
   var limit = new Date();
   limit = limit.setMilliseconds(limit.getMilliseconds() + ms);
   while ((new Date()) < limit) {
      // do nothing
      ;
   }
}


let table = base.getTable("EXM"); //The name of the table you're in here
let record = await input.recordAsync('Pick a record', table);
if (record) {

   if (record.getCellValue("switch") == null) {
      table.updateRecordAsync(record, { 'switch': true });
      output.text('checkbox ticked');
   }

   delay(2000);
   table.updateRecordAsync(record, { 'switch': false });
   output.text('2 second interval');

}




See Solution in Thread

6 Replies 6
Zack_S
8 - Airtable Astronomer
8 - Airtable Astronomer

Hi @Sean_Patterson1,

What’s the purpose for your delay? I understand setting delays can sometimes cause other issues.

However, this may work for you.
Also, notice how this line if (record.getCellValue("switch") == null) is equal to null and not false.

function delay(ms) {
   var limit = new Date();
   limit = limit.setMilliseconds(limit.getMilliseconds() + ms);
   while ((new Date()) < limit) {
      // do nothing
      ;
   }
}


let table = base.getTable("EXM"); //The name of the table you're in here
let record = await input.recordAsync('Pick a record', table);
if (record) {

   if (record.getCellValue("switch") == null) {
      table.updateRecordAsync(record, { 'switch': true });
      output.text('checkbox ticked');
   }

   delay(2000);
   table.updateRecordAsync(record, { 'switch': false });
   output.text('2 second interval');

}




Welcome to the Airtable community!

If you are using scripting extension (versus an automation script), you can use setTimeout even though it has the squiggly red underlines, and the script will still work.

If you are using an automation script, you cannot use setTimeout.

I have an automation that looks for certain cell conditions - basically creates a Jira ticket once relevant field conditions are met.

The conditions for the automation are:

  1. Status is set to - Dev
  2. Checkbox is true (button click)
  3. Jira ref field is empty (to make sure we don’t create duplicates) - returned from Jira when issue created

The reason I want the checkbox to reset to false, is that in the unlikely scenario I delete the Jira reference (and don’t change the other fields), the automation then creates another duplicate ticket as the above conditions are all met.

Ok, that makes sense. Does the script above work for you?

So in the automation, you wouldn’t be able to update the record and set the “switch” checkbox to false?

Ah yeah great idea, didn’t even think to do that. Works perfectly.

One other thing. I’ve created an If statement at the beginning to determine if certain cell values (record status and if a Jira reference URL is empty) match before executing the script. I know I can do this in the automation conditions - and have done so, but I also want to set this here so I can output a message to the user if the script does not run and why

When I include the statement:

 if (record.getCellValue("Status") == "Waiting for Dev" && record.getCellValue("Jira Reference") !== null)

I get the following error message with a red underline and it doesn’t work for any record, even when the condition is met:

‘This condition will always return ‘false’ since the types ‘{ id: “sel4blGk0XQdH4GNq”; name: “Idea Submission”; color: “redDark1”; } | { id: “selRAX1oSmEDvQEWh”; name: “Exploration”; color: “grayBright”; } | { id: “selVICEl967O7OYjB”; name: “Ideation”; color: “purpleBright”; } | … 8 more … | { …; }’ and ‘string’ have no overlap.(2367)’

This is the code in full:

let table = base.getTable("EXM"); //The name of the table you're in here
let record = await input.recordAsync('Pick a record', table);
if (record) {

   if (record.getCellValue("Status") == "Waiting for Dev" && record.getCellValue("Jira Reference") !== null){
   	if (record.getCellValue("switch (DO NO DELETE)") == null) {
      table.updateRecordAsync(record, { 'switch (DO NO DELETE)': true });
      output.text("TICKET CREATED");
   }
}
else output.text("ERROR: THIS JIRA TICKET CANNOT BE CREATED. \n\n Check the following: \n\n 1. Status field is set to Waiting for Dev \n 2. Jira ticket may already exist - look for a Jira URL in the Jira Reference field. If this is not the right Jira URL/ticket, delete the contents of the Jira reference field and try again");
}

Change record.getCellValue("Status") to record.getCellValueAsString("Status")

Also please mark this as the solution if it worked