Can you define functions in Execute Code cards?
# 🤝help
k
e.g. let's say I want to be able to call the following function multiple times in my workflow:
Copy code
js
function sumRange(a, b) {
    // Ensure a is less than or equal to b
    if (a > b) {
        return 0;
    }

    let sum = 0;
    for (let i = a; i <= b; i++) {
        sum += i;
    }

    return sum;
}
Would I be able to define this function somehow so that I can use it later? I tried simply entering this code into an Execute Code card in the beginning of my workflow, but when calling this function later, it gives me an error saying sumRange is not defined
f
You can't. Each execute code card is a fresh new coding space.
n
What if you save your function as a string into a variable and then call eval to invoke it as needed? // Define the function as a string let functionString = ` return function greet(name) { return "Hello, " + name; }; `; // Store the function string let storedFunctionString = functionString; // Later, retrieve and create the function let greet = new Function(storedFunctionString)(); // Now the function is defined and can be called let result = greet("John"); console.log(result); // Output: Hello, John
put it in like user.function_greet or in workflow or something similar. maybe bot even. bot.function_greet
f
I suppose yea. And you should use the bot scope.
n
Do you think a custom integration would work for this? On first glance it seems so
f
An integration for 1 function? Or for a library for them?
n
well theoretically 1 function but the idea would be a library. For example I have a bunch of helper functions Ive coded up that I like to drop into projects.
f
You could just make a card for each function.
n
idea is they need to be globally scoped
f
@straight-wolf-37371 integration idea