Help

Automation Scripting – TypeError when executing code from a string.

Topic Labels: Scripting
3054 5
cancel
Showing results for 
Search instead for 
Did you mean: 
coltsh3lby
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello everybody,

I have a table that's thousands of lines long, with a column called "Formula" which contains small string expressions like:

amount = salary * 0.11;

What I'm trying to do is run this string in my code, so that it updates the `amount` variable based on which line I select. However, I am running into an error when I try to run the string as code.

This is my code, which is able to compile with the correct answer in other online compilers. However, when I run this in my automation script (specifically the line where I call `func()`) I receive TypeError: func is not a function.

let code = "amount = salary * 0.11;";

// Define the function with the desired arguments
let func = new Function('salary', code);

// Call the function with the desired argument values
func(50000);
console.log(amount);

I've tried using `eval()` to run this (I know it is not best practice) but similarly received the error ReferenceError: eval is not defined.
 
There has to be some way to get this code to run in my automation script, I just have no idea how to do this. Why does this compile in other compilers, but Airtable can seemingly not recognize the function constructor?
 
Anything helps, thanks!
5 Replies 5

eval() is not available in automation scripts. This was a specific choice that Airtable made when implementing automation scripts.

Hi Kuovonne,

Thanks for your quick reply. I understand this, and this is why I’m attempting to use a function constructor instead to accomplish the same thing. Is this explicitly forbidden, like using eval() is? I feel like it shouldn’t be


I understand this, and this is why I’m attempting to use a function constructor instead to accomplish the same thing. Is this explicitly forbidden, like using eval() is? I feel like it shouldn’t be

It's the same concept. Airtable doesn't like automation scripts to run code generated at run-time. If you feel that the behavior should be different, you can submit a feature request to Airtable support.

If you do not need the code to run automatically, you can run your code from a button in Scripting Extension. Scripting extension does not have the same limitations as automation scripts.

Unfortunately, it needs to run automatically. Is there ways to automate button presses using the scripting extension, or something you know of that accomplishes something similar? None of these calculations are super complicated on their face, but I’d love to avoid the alternative of creating some sort of language that needs to be parsed by JavaScript on a much lower level. I appreciate the guidance