Sep 22, 2022 09:00 AM
I’m trying to create a script in Airtable linked to a button. The process is as follows:
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?
Solved! Go to Solution.
Sep 22, 2022 11:00 AM
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');
}
Sep 22, 2022 11:00 AM
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');
}
Sep 22, 2022 01:38 PM
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
.
Sep 23, 2022 02:07 AM
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:
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.
Sep 23, 2022 05:22 AM
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?
Sep 23, 2022 05:48 AM
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");
}
Sep 23, 2022 06:11 AM
Change record.getCellValue("Status")
to record.getCellValueAsString("Status")
Also please mark this as the solution if it worked