Frappe Client: Need help using Submit()

Hello

I am using the FrappeClient library to insert data into ERPNext.

I’m trying to reset all items to 0 quantity using “Stock Entry” with “Material Issue”

So here is my code for using the FrappeClient:

NAMING_SERIES = "STE-"
PURPOSE = "Material Issue"
COMPANY = "Patriot"
CONVERSION_FACTOR = "1.0"
ITEM_CODE = 0

def sync():
	print "logging in..."
	client = FrappeClient("https://erp.erpnext.com", "administrator", "password")

	with open("resetstock.csv", "rU") as jobsfile:
		reader = csv.reader(jobsfile, dialect='excel')
		count = 0
		for row in reader:
			if count == 0:
				count = 1
				continue

			print "Finding Item: " + row[ITEM_CODE]
			item = client.get_value("Item", "name", {"item_code": row[ITEM_CODE]})

			if item:
				itemDoc = client.get_doc("Item", item["name"])
			else:
				print "Item not found moving the next item!"
				continue

			itemQty = str(itemDoc["opening_stock"])
			itemUOM = str(itemDoc["stock_uom"])

			resetItemDoc = {"doctype":"Stock Entry"}
			resetItemDoc["naming_series"] = NAMING_SERIES
			resetItemDoc["purpose"] = PURPOSE
			resetItemDoc["company"] = COMPANY
			resetItemDoc["from_warehouse"] = "Stores - PM"

			stockEntryDetailDoc = {"doctype":"Stock Entry Detail"}
			stockEntryDetailDoc["item_code"] = row[ITEM_CODE]
			stockEntryDetailDoc["qty"] = itemQty
			stockEntryDetailDoc["uom"] = itemUOM
			stockEntryDetailDoc["conversion_factor"] = CONVERSION_FACTOR
			stockEntryDetailDoc["stock_uom"] = itemUOM
			stockEntryDetailDoc["transfer_qty"] = itemQty

			resetItemDoc["items"] = []
			resetItemDoc["items"].append(stockEntryDetailDoc)
			
			if resetItemDoc.get("status") != "Rejected":
				resetItemDoc["status"] = "Filled Form"

			insertedDoc = client.insert(resetItemDoc)
			client.submit(insertedDoc)
			print "Inserted"

The code works fine till when it hit client.submit()

The error I am getting is:

  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 57, in application
    response = frappe.handler.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 22, in handle
    data = execute_cmd(cmd)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 53, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 923, in call
    return fn(*args, **newargs)
TypeError: submit() takes exactly 1 argument (0 given)

Don’t know if I used the submit wrong or there is an internal code bug.

Please help. thank you

Hey @Jason_Lin

I didn’t really follow your code but check this

You see need a doclist to work

The second an most important is that in your line “insertedDoc” you get the return value of

self.post_process(res) , So you don’t actually get any doctype… but a message or data or None

I had to bold my comments to stand out from github code pasting.

1 Like

Good Day

Do not know how i could implement the submit in Attendance:

for row in cur:
doc = {“doctype”:“Attendance”}
doc[“employee”] = row[0]
doc[“attendance_date”] = str(row[1].date())

if row[2] == 0:
            doc["status"] = "Present"
elif row[2] == 1:
            doc["status"] = "Absent"
elif row[2] == 2:
            doc["status"] = "Absent"

if len(row[3]) == 13:
	smt = client.get_value("Attendance","attendance_date",filters={"attendance_date": doc["attendance_date"], "employee": doc["employee"] })
	if smt is None: 
		doc = {"doctype": "Attendance",
   			       "employee": doc["employee"],
   			       "attendance_date": doc["attendance_date"],
   			       "status": doc["status"],
   			       "company": "Standerton Mills"}

		record = client.insert(doc)
		client.submit(record) # ? client.submit(doclist)

		

		# print "Added: " + row[3] + "-" + doc["attendance_date"] + "-" + doc["status"]
        else:
		print "ALREADY Added: " + row[3] + "-" + doc["attendance_date"] + "-" + doc["status"]

Any Help Please?

Thank You

Albertus Geyser

I don’t know if it works, I haven’t checked it.

client.submit([record]) # make it a list with a single doc.

Thank you for all your replies. What is considered a doclist? I’ve tried to put the doc name or is into array but that didn’t work.

a list with docs …

like [ frappe.new_doc(‘Employee’), frappe.new_doc(‘Employee’)].

I dont know if i make it clear or more confusing ?

Good Day

I tried: client.submit([record]), with no luck.
Partial error message:

"doclist": json.dumps(doclist)

File “/usr/local/nagios/libexec/frappeclient/frappeclient.py”, line 190, in post_request
res = self.post_process(res)
File “/usr/local/nagios/libexec/frappeclient/frappeclient.py”, line 203, in post_process
rjson = response.json()
File “/usr/local/lib/python2.7/dist-packages/requests/models.py”, line 892, in json
return complexjson.loads(self.text, **kwargs)
File “/usr/lib/python2.7/json/init.py”, line 338, in loads
return _default_decoder.decode(s)
File “/usr/lib/python2.7/json/decoder.py”, line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python2.7/json/decoder.py”, line 384, in raw_decode
raise ValueError(“No JSON object could be decoded”)
ValueError: No JSON object could be decoded

Could anyone shed light on submit?

Thank You
Albertus Geyser

Albertus

I haven’t figured how to do Submit yet. but you are looking at the wrong error message. The real errors are within the HTML codes that you get on your terminal.

It is unclear to me what datatypes the client.submit function is expecting.
A dictionary doesn’t work: {"name"; "record"}
A list doesn’t work: ["record"]
A whole document doesn’t work: submit_record = client.get_doc(submit_doctype, name=name["name"])
client.submit(submit_record)

All I get is the same error: TypeError: submit() takes exactly 1 argument (0 given)
Or when I try:
client.submit(submit_doctype, name)
I get:
TypeError: submit() takes 2 positional arguments but 3 were given

1 Like

So the answer to this is that it’s looking for a JSON-esque record to submit, eg the output of get_doc … make your changes to the record and then doc.submit()

That’s what the documentation says, but I missed it this first ten times. Hopefully this answer will help the next person struggling with this.

1 Like