Not able to create a new document with Child Table from Server Script

Server Script to create a purchase receipt with child table:

pr = frappe.new_doc("Purchase Receipt")
pr.update(
    {
        "supplier": doc.supplier, "supplier_name": doc.supplier_name, "posting_date": doc.posting_date, "posting_time": doc.posting_time
        
    }
)

for d in doc.get('items'):
   if d.item_code:
        party_row = pr.append(
		"items",
		{
		    "item_code" : d.item_code,
	        "description" : d.description,
            "qty": flt(d.qty),
            "uom": d.uom,
	        "stock_uom": d.stock_uom,
	        "conversion_factor": flt(d.conversion_factor),
            "rate": d.rate,
            "amount": d.amount,
            "base_rate": d.base_rate,
	    "base_amount" : d.base_amount
	   },
	   )
pr.save()
pr.submit()

Log file:

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 55, in handle
    return frappe.handler.handle()
  File "apps/frappe/frappe/handler.py", line 37, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 75, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1447, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/desk/form/save.py", line 23, in savedocs
    doc.submit()
  File "apps/frappe/frappe/model/document.py", line 1018, in submit
    return self._submit()
  File "apps/frappe/frappe/model/document.py", line 1007, in _submit
    return self.save()
  File "apps/frappe/frappe/model/document.py", line 310, in save
    return self._save(*args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 364, in _save
    self.run_post_save_methods()
  File "apps/frappe/frappe/model/document.py", line 1088, in run_post_save_methods
    self.run_method("on_submit")
  File "apps/frappe/frappe/model/document.py", line 945, in run_method
    run_server_script_for_doc_event(self, method)
  File "apps/frappe/frappe/core/doctype/server_script/server_script_utils.py", line 38, 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 90, in execute_doc
    safe_exec(self.script, _locals={"doc": doc}, restrict_commit_rollback=True)
  File "apps/frappe/frappe/utils/safe_exec.py", line 64, in safe_exec
    exec(compile_restricted(script), exec_globals, _locals)  # pylint: disable=exec-used
  File "<unknown>", line 1, in <module>
  File "apps/frappe/frappe/utils/safe_exec.py", line 36, in default_function
    raise AttributeError(f"module has no attribute '{key}'")
AttributeError: module has no attribute 'new_doc'

Error message pop up

image

changed the code to
pr = frappe.get_doc(dict(“Purchase Receipt”)).insert() … instead of frappe.new_doc()
pr.update(
{
“supplier”: doc.supplier, “supplier_name”: doc.supplier_name, “posting_date”: doc.posting_date, “posting_time”: doc.posting_time

}

)

Error changed:

with the following server script I was able to create Purchase Receipt Doctype with Child table.
Note the child table from one Purchase Receipt was copied to another new Purchase receipt

for d in doc.get('items'):
   if d.item_code:
       pr = frappe.get_doc({
        "doctype" : "Purchase Receipt",
        "supplier": doc.supplier, 
        "supplier_name": doc.supplier_name, 
        "posting_date": doc.posting_date, 
        "posting_time": doc.posting_time,
		"items": [
		    {
		    "item_code" : d.item_code,
	        "description" : d.description,
            "qty": d.qty,
            "uom": d.uom,
	        "stock_uom": d.stock_uom,
	        "conversion_factor": d.conversion_factor,
            "rate": d.rate,
            "amount": d.amount,
            "base_rate": d.base_rate,
	        "base_amount" : d.base_amount
	        }
	   ]
	   })
pr.insert()
pr.save()
1 Like