Import Error in Server Script Written Under a DocType

Hello there,
I am working on customizing ERPNext and I have a custom DocType created called “Coffee Gathering Receipt”. And this DocType has a field called “total_price” which stores the total price of the coffee that is going to be store in warehouse. Now whenever a new document of this DocType is created, I need this total_price to be stored in the “Stock In Hand Account”, in the chart of accounts, as well.

To the DocType, I added the following custom script with the following configuration.

  • Script Type:

DocType Event

  • Module (for export):

Accounts

  • Reference Document Type:

Coffee Gathering Receipt

  • DocType Event:

After Save

  • Script:
from frappe import _

def validate(doc, method):
    stock_in_hand_account = frappe.get_doc("Account", {"account_name": "Stock In Hand"})
    stock_in_hand_account.balance = stock_in_hand_account.balance + doc.quantity
    stock_in_hand_account.save(ignore_permissions=True)

But now, after setting this, when I create a new document of the Coffee Gathering Receipt DocType, the following error appears. Here is the complete log.

App Versions

{
	"erpnext": "14.33.2",
	"frappe": "14.43.1",
	"hrms": "15.0.0-dev",
	"payments": "0.0.1"
}

Route

Form/Coffee Gathering Recipt/bd9dee727e

Traceback

Traceback (most recent call last):
  File "apps/frappe/frappe/app.py", line 94, 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 47, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 85, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1620, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/desk/form/save.py", line 28, in savedocs
    doc.save()
  File "apps/frappe/frappe/model/document.py", line 305, in save
    return self._save(*args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 356, in _save
    self.run_post_save_methods()
  File "apps/frappe/frappe/model/document.py", line 1082, in run_post_save_methods
    self.run_method("on_update")
  File "apps/frappe/frappe/model/document.py", line 918, 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 103, in execute_doc
    safe_exec(self.script, _locals={"doc": doc}, restrict_commit_rollback=True)
  File "apps/frappe/frappe/utils/safe_exec.py", line 72, in safe_exec
    exec(compile_restricted(script), exec_globals, _locals)  # pylint: disable=exec-used
  File "<unknown>", line 6, in <module>
ImportError: __import__ not found

Request Data

{
	"type": "POST",
	"args": {
		"doc": "{\"name\":\"bd9dee727e\",\"owner\":\"Administrator\",\"creation\":\"2023-08-10 08:02:23.318131\",\"modified\":\"2023-08-10 12:01:07.936031\",\"modified_by\":\"Administrator\",\"docstatus\":0,\"idx\":0,\"eu\":1,\"nop\":0,\"jas\":1,\"cafe\":0,\"no\":\"245\",\"date\":\"2023-08-10\",\"name1\":\"Birhane\",\"gender\":\"MALE\",\"kebele\":\"10\",\"type_of_coffee_product\":\"Honey\",\"quantity\":111,\"price\":34,\"total_price\":\"2000\",\"purchase_employee\":\"Administrator\",\"name_of_warehouse\":\"Xerro\",\"doctype\":\"Coffee Gathering Recipt\",\"__last_sync_on\":\"2023-08-10T09:09:33.713Z\",\"__unsaved\":1}",
		"action": "Save"
	},
	"btn": {
		"jQuery360069523876303124541": {
			"events": {
				"click": [
					{
						"type": "click",
						"origType": "click",
						"guid": 904,
						"namespace": ""
					}
				]
			}
		}
	},
	"freeze": true,
	"headers": {},
	"error_handlers": {},
	"url": "/api/method/frappe.desk.form.save.savedocs"
}

Response Data

{
	"exception": "ImportError: __import__ not found"
}

Can anyone help me out solving this problem?
Thanks.

Hi @NatnaelTilaye,

Some import package and method does not work on the server side.

So please create a custom app and then check it.

Thank You!

Thank you @NCP ,
I will do that. In the meantime, I have been wondering if it is possible to link the data on a field of a doctype to a specific account in the Chart of Accounts without writing code in a custom app?

Thanks,

I think, not possible.

I think is not needed that import …

@avc When I remove the import definition there is no error coming up but it does not update the value in the account.

Hi @NatnaelTilaye,

Please check it and apply it.

Maybe It works.

Set your doctype according to.

Thank You!

Hi:

  1. In server script just write the code without def …
    stock_in_hand_account = frappe.get_doc("Account", {"account_name": "Stock In Hand"})
    stock_in_hand_account.balance = stock_in_hand_account.balance + doc.quantity
    stock_in_hand_account.save(ignore_permissions=True)
  1. I can’t understand the use case … but … There is not a “balance” field on the Account Doctype … so this code will not work. The account balance is the result of debit and credit operations (from invoices, payments, stock entries, etc …) wich are stored in other doctypes.

Hope this helps.

Thank you for the clarification @avc
The idea for this script is to add the value of the field, named “quantity” of the Coffee Gathering Receipt doctype, to the Stock In Hand account whenever a new document is created.

But if the account does not have a “balance” field, then how can I add the field value to the account whenever a new document of the doctype is created?

Hi:

It’s a bit complex, to get accounts updated you will need to manually create GL Entries, and so …

Your “Coffee Gathering Receipt” doctype is a kind of stock/money operation, right? So … why not use some ERPNext doctype like Invoices and customize it instead make your “own” way? Or create with a script the desired document from your doctype. This way all the following processes will be automatic.

Hope this helps.