Hi, I am doing in-app server side script. I wrote a recursive function, which calls itself. It worked when I was trying on System Console. But when I transfered it to Server Side Script - it gave an error saying that function itself isn’t defined.
There are some quirks to the Restricted Python library used by the safe_exec environment that causes variable scope to sometimes get lost. The main place I’ve seen this is in list comprehensions, but it seems to happen in recursion as well.
You might be able to fix the issue by adding the function name to the global scope. This, for example, works for me in an API-type server script:
num = int(frappe.form_dict.message) global factorial # <-- add this line to avoid scope problems def factorial(x): if x == 1: return 1 else: return (x * factorial(x-1)) frappe.response['message'] = factorial(num)