I’m trying to use the server script function however when I try to create a file and import frappe I get this error when running:
Is there no other solution?
Please share the code, Let’s check, It is capable for server script doctype or not.
### App Versions
{
“erpnext”: “15.29.2”,
“frappe”: “15.33.3”,
“hrms”: “16.0.0-dev”
}
### Route
Form/Task/new-task-gjkihvbyus
### Traceback
Traceback (most recent call last):
File “apps/frappe/frappe/app.py”, line 114, in application
response = frappe.api.handle(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “apps/frappe/frappe/api/init.py”, line 49, in handle
data = endpoint(**arguments)
^^^^^^^^^^^^^^^^^^^^^
File “apps/frappe/frappe/api/v1.py”, line 36, in handle_rpc_call
return frappe.handler.handle()
^^^^^^^^^^^^^^^^^^^^^^^
File “apps/frappe/frappe/handler.py”, line 49, 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 1768, in call
return fn(*args, **newargs)
^^^^^^^^^^^^^^^^^^^^
File “apps/frappe/frappe/utils/typing_validations.py”, line 31, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File “apps/frappe/frappe/client.py”, line 227, in save
doc.save()
File “apps/frappe/frappe/model/document.py”, line 337, in save
return self._save(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “apps/frappe/frappe/model/document.py”, line 359, in _save
return self.insert()
^^^^^^^^^^^^^
File “apps/frappe/frappe/model/document.py”, line 285, in insert
self.run_method(“before_insert”)
File “apps/frappe/frappe/model/document.py”, line 966, in run_method
run_server_script_for_doc_event(self, method)
File “apps/frappe/frappe/core/doctype/server_script/server_script_utils.py”, line 42, 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 169, in execute_doc
safe_exec(
File “apps/frappe/frappe/utils/safe_exec.py”, line 111, in safe_exec
exec(
File “: task_project”, line 1, in
ImportError: import not found
### Request Data
{
“type”: “POST”,
“args”: {
“doc”: “{"docstatus":0,"doctype":"Task","name":"new-task-gjkihvbyus","__islocal":1,"__unsaved":1,"owner":"quynhmai.nguyen@vnc.vnconference.vn","is_group":0,"is_template":0,"status":"Open","priority":"Low","expected_time":0,"is_milestone":0,"depends_on":,"company":"Công ty cổ phần tổ chức hội nghị Việt Nam","__run_link_triggers":1,"subject":"test 3","project":"PROJ-0006"}”
},
“freeze”: true,
“headers”: {},
“error_handlers”: {},
“url”: “/api/method/frappe.client.save”,
“request_id”: null
}
### Response Data
{
“exception”: “ImportError: import not found”,
“exc_type”: “ImportError”,
“_exc_source”: “Server Script”
}
remove the first 2 lines and set the indentation
if doc.project:
# your code
And check it.
I love this. great! Can you explain more about this writing style?
Don’t use import, just enter the simple code but some limitations are set in the server script doctype.
Because server scripts run in a restricted context for ensuring security. You can’t import any external python module in server scripts.
Good. So what do you think about updating the doctype workflow? Can this be done via server script? or client script? For example, I want the doctype task to be editable once submitted (I’m in development mode so I can customize is Is Submittable) or when a project is completed, all its remaining tasks will go into cancelable mode. Are not?
If the script is small, use a server script; for larger developments or more complex requirements, create the server script in your custom app. Not everything can be achieved with just a client script or server script. Sometimes, you need to work on both sides. If you want to make a doctype editable, you’ll need to use a client script, while other scenarios should be handled on the server side.
Greet. I’m trying to write a simple code that says when a project switches workflows (pre-set in the workflow), I want to affect all of its subtasks. Do you have any method?
if doc.workflow_state == 'Completed':
tasks = frappe.get_all('Task', filters={'project': doc.name}, fields=['name', 'workflow_state'])
for task in tasks:
if task.workflow_state != 'Completed':
workflow_state = frappe.get_value("Workflow State", {"state": 'Overdue'}, "name")
if not workflow_state:
frappe.throw(_("Workflow State not found for status {0}").format('Overdue'))
task_doc = frappe.get_doc("Task", task.name)
task_doc.workflow_state = workflow_state
task_doc.save(ignore_permissions=True)
frappe.db.commit()