Not able to call a user-defined function from another user-defined function in server side script

I get the error saying the function is not defined when I try to to call a user-defined function from another user-defined function in server-side script.
The error says function not defined.
I am using the ERPNext interface for the scripting. How do I connect these functions? If I define a class, it gives me
“NameError: name ‘metaclass’ is not defined”

please send us the error output.

Hi, this is the error when I put both the user defined functions in a class.

App Versions

{
	"erpnext": "14.5.1",
	"frappe": "14.14.2",
	"non_profit": "0.0.1"
}

Route

Form/Batch/YRNWHT-0001

Trackeback


Traceback (most recent call last):
  File "apps/frappe/frappe/app.py", line 69, in application
    response = frappe.api.handle()
  File "apps/frappe/frappe/api.py", line 54, in handle
    return frappe.handler.handle()
  File "apps/frappe/frappe/handler.py", line 45, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 83, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1585, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/desk/form/save.py", line 23, in savedocs
    doc.save()
  File "apps/frappe/frappe/model/document.py", line 303, in save
    return self._save(*args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 354, in _save
    self.run_post_save_methods()
  File "apps/frappe/frappe/model/document.py", line 1093, in run_post_save_methods
    self.run_method("on_update")
  File "apps/frappe/frappe/model/document.py", line 934, in run_method
    run_server_script_for_doc_event(self, method)
  File "apps/frappe/frappe/core/doctype/server_script/server_script_utils.py", line 39, in run_server_script_for_doc_event
    frappe.get_doc("Server Script", script_name).execute_doc(doc)
  File "apps/frappe/frappe/core/doctype/server_script/server_script.py", line 98, in execute_doc
    safe_exec(self.script, _locals={"doc": doc}, restrict_commit_rollback=True)
  File "apps/frappe/frappe/utils/safe_exec.py", line 69, in safe_exec
    exec(compile_restricted(script), exec_globals, _locals)  # pylint: disable=exec-used
  File "<unknown>", line 1, in <module>
NameError: name '__metaclass__' is not defined

Request Data

{
	"type": "POST",
	"args": {
		"doc": "{\"name\":\"YRNWHT-0001\",\"owner\":\"Administrator\",\"creation\":\"2022-10-22 14:15:35.509024\",\"modified\":\"2022-11-11 14:59:03.206761\",\"modified_by\":\"chaitrad303@gmail.com\",\"docstatus\":0,\"idx\":0,\"disabled\":0,\"use_batchwise_valuation\":1,\"batch_id\":\"YRNWHT-0001\",\"item\":\"Yarn-White\",\"item_name\":\"Yarn-White\",\"manufacturing_date\":\"2022-10-05\",\"batch_qty\":0.1,\"stock_uom\":\"Kg\",\"reference_doctype\":\"Stock Entry\",\"reference_name\":\"MAT-STE-2022-00004\",\"qty_to_produce\":0,\"produced_qty\":0,\"doctype\":\"Batch\",\"linked_ids\":[],\"__last_sync_on\":\"2022-11-11T11:32:24.763Z\",\"__unsaved\":1}",
		"action": "Save"
	},
	"btn": {
		"jQuery360033997854411796971": {
			"events": {
				"click": [
					{
						"type": "click",
						"origType": "click",
						"guid": 405,
						"namespace": ""
					}
				]
			}
		}
	},
	"freeze": true,
	"headers": {},
	"error_handlers": {},
	"url": "/api/method/frappe.desk.form.save.savedocs"
}

Response Data

{
	"exception": "NameError: name '__metaclass__' is not defined"
}

If I do not use any class, and call first user defined function get_doctype() from the next user defined function, I get the following error -

App Versions

{
	"erpnext": "14.5.1",
	"frappe": "14.14.2",
	"non_profit": "0.0.1"
}

Route

Form/Batch/YRNWHT-0001

Trackeback

Traceback (most recent call last):
  File "apps/frappe/frappe/app.py", line 69, in application
    response = frappe.api.handle()
  File "apps/frappe/frappe/api.py", line 54, in handle
    return frappe.handler.handle()
  File "apps/frappe/frappe/handler.py", line 45, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 83, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1585, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/desk/form/save.py", line 23, in savedocs
    doc.save()
  File "apps/frappe/frappe/model/document.py", line 303, in save
    return self._save(*args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 354, in _save
    self.run_post_save_methods()
  File "apps/frappe/frappe/model/document.py", line 1093, in run_post_save_methods
    self.run_method("on_update")
  File "apps/frappe/frappe/model/document.py", line 934, in run_method
    run_server_script_for_doc_event(self, method)
  File "apps/frappe/frappe/core/doctype/server_script/server_script_utils.py", line 39, in run_server_script_for_doc_event
    frappe.get_doc("Server Script", script_name).execute_doc(doc)
  File "apps/frappe/frappe/core/doctype/server_script/server_script.py", line 98, in execute_doc
    safe_exec(self.script, _locals={"doc": doc}, restrict_commit_rollback=True)
  File "apps/frappe/frappe/utils/safe_exec.py", line 69, in safe_exec
    exec(compile_restricted(script), exec_globals, _locals)  # pylint: disable=exec-used
  File "<unknown>", line 55, in <module>
  File "<unknown>", line 16, in get_source
NameError: name 'get_doctype' is not defined

Request Data

{
	"type": "POST",
	"args": {
		"doc": "{\"name\":\"YRNWHT-0001\",\"owner\":\"Administrator\",\"creation\":\"2022-10-22 14:15:35.509024\",\"modified\":\"2022-11-11 14:59:03.206761\",\"modified_by\":\"chaitrad303@gmail.com\",\"docstatus\":0,\"idx\":0,\"disabled\":0,\"use_batchwise_valuation\":1,\"batch_id\":\"YRNWHT-0001\",\"item\":\"Yarn-White\",\"item_name\":\"Yarn-White\",\"manufacturing_date\":\"2022-10-29\",\"batch_qty\":0.1,\"stock_uom\":\"Kg\",\"reference_doctype\":\"Stock Entry\",\"reference_name\":\"MAT-STE-2022-00004\",\"qty_to_produce\":0,\"produced_qty\":0,\"doctype\":\"Batch\",\"linked_ids\":[],\"__last_sync_on\":\"2022-11-11T14:02:51.098Z\",\"__unsaved\":1}",
		"action": "Save"
	},
	"btn": {
		"jQuery360040136190226226011": {
			"events": {
				"click": [
					{
						"type": "click",
						"origType": "click",
						"guid": 283,
						"namespace": ""
					}
				]
			}
		}
	},
	"freeze": true,
	"headers": {},
	"error_handlers": {},
	"url": "/api/method/frappe.desk.form.save.savedocs"
}

Response Data

{
	"exception": "NameError: name 'get_doctype' is not defined",
	"_server_messages": "[\"{\\\"message\\\": \\\"Batch ID  is   YRNWHT-0001\\\", \\\"title\\\": \\\"Message\\\"}\", \"{\\\"message\\\": \\\"Batch\\\", \\\"title\\\": \\\"Message\\\"}\"]"
}

Hello,

Server scripts only allow access to subset of functionality. You can find it here: Script API

So this means I cannot call a function from another function within the same script in the case of in-app scripting?

You can. You’re probably creating a class in script? That isn’t allowed.

__metaclass__ error is most likely from use of classes.

Creating a class was something I tried after I could not call a function from another function.
I have written them one below another. In the same in-app server-side script. When I call the function, it says it is not defined. That’s why I am trying to get some way I can do this. I want to have one recursive function that calls another function for some info.

I don’t want to use the concept of class if not required.

Very difficult to guess what exactly you are doing. Can you share a minimal reproducible example? Doesn’t have to be exact code.

def test():
return 2

def get_source():
b = test()
frappe.msgprint(b)
frappe.msgprint("Inside get_source() ")

Here I am trying to call test() from get_source().
It gives me an error saying test() is not defined.

I have an experience where we can not use an imported function in server script doc type of UI , if we write in the .py file of that document then we can use the imported function without any issue.

So you can move ahead this way also

I am not importing. I am writing them together like below -
def test():
return 2

def get_source():
b = test()
frappe.msgprint(b)
frappe.msgprint("Inside get_source() ")

Here I am trying to call test() from get_source().
It gives me an error saying test() is not defined.

Hello,

I am struggeling with the scheduling stuff in frappe.
It is at this time a showstopper.

I try to call a function in a custom app i have written in a serverside script i cant access the app or the function i wrote there.

Another trial was using hooks.py, but this was a complete failure “Event all does not exist!” i get when trying to trigger the ecent all from the bench cli.
Also a bench migrate caused no effect.

Has anyone an idea how to realize scheduling in frappe calling a function in a custom app?
I accept also external solutions, because this point is a showstopper.