Error on FrappeClient API call

Greetings,

I am getting the following error while calling the client.insert method.

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 62, 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 939, in call
    return fn(*args, **newargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/desk/form/save.py", line 19, in savedocs
    doc.submit()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 831, in submit
    self._submit()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 820, in _submit
    self.save()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 256, in save
    return self._save(*args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 307, in _save
    self.run_post_save_methods()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 890, in run_post_save_methods
    self.run_method("on_submit")
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 755, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 1024, in composer
    return composed(self, method, *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 1007, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 749, in <lambda>
    fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
  File "/home/frappe/frappe-bench/apps/waterman/waterman/waterman/doctype/fish_weights/fish_weights.py", line 115, in on_submit
    client.insert(invoice_doc)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/frappeclient/frappeclient.py", line 53, in insert
    return self.post_process(res)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/frappeclient/frappeclient.py", line 203, in post_process
    rjson = response.json()
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/requests/models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

my code looks like this:

# -*- coding: utf-8 -*-
# Copyright (c) 2018, oasis and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappeclient import FrappeClient
class FishWeights(Document):
	def on_submit(self):
	    client = FrappeClient("url", "user", "password")
            #self.create_customer()
            invoice_doc = {
                "doctype": "Purchase Invoice",
                "customer": self.supplier,
                "company": "Waterman",
                "due_date": "2018-12-30",
                "currency": "UGX",
                "conversion_rate": 1.0,
                'buying_price_list': 'Standard Buying',
                'price_list_currency': 'UGX',
                'plc_conversion_rate': 1.0,
                'naming_series': 'PINV-',
                'base_net_total':0,
                'base_grand_total':0,
                'base_total':0,
                'grand_total':0,
                'credit_to': "2110 - Creditors - W",
                #"submit_on_creation": 1,
                "docstatus": 1,
                'items':[{
                    'qty': self.lessthree_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '0-3kgs',
                    'description': "Small",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.four_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '4kgs',
                    'description': "4Kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.fivetonine_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '5-9kgs',
                    'description': "5-9kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.tenfourteen_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '10-14kgs',
                    'description': "10-14kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.fifteennineteen_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '15-19kgs',
                    'description': "15-19kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.twentytonine_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '20-29kgs',
                    'description': "20-29kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.thirtytonine_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '30-39kgs',
                    'description': "30-39kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.fourtytonine_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '40-49kgs',
                    'description': "40-49kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.morefifty_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '50-100kgs',
                    'description': "50-100kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                }]}

            client.insert(invoice_doc)

Any help is welcome

Hello,

I added simplejson to try and see a clear error, then this one is the one i am receiving:

  File "/home/frappe/frappe-bench/apps/waterman/waterman/waterman/doctype/fish_weights/fish_weights.py", line 120, in on_submit
    simplejson.loads(invoice_doc)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/simplejson/decoder.py", line 395, in raw_decode
    ord0 = ord(s[idx])
KeyError: 0

Any help?

There’s this for clues Frappe Client: Need help using Submit() - #7 by Albertus

Another thought -

fish_weights.py is defined, but also a fish_weights.json as expected too?

FWIW this says, to create these, to not select custom Where is the DocType code of the new Model?

Thanks for your reply,

I am not trying to create and submit the purchase invoice, just create it in draft once the fish_weights doctype is submitted.

The .py and json files are there and i checked the custom isnt selected.
This is the current code, as you can see i just added the simplejson.loads at the end to get a clearer error.

# -*- coding: utf-8 -*-
# Copyright (c) 2018, oasis and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappeclient import FrappeClient
import traceback
import simplejson
class FishWeights(Document):
    def on_submit(self):
        client = FrappeClient("url", "user", "pass")
        invoice_doc = {
                "doctype": "Purchase Invoice",
                "customer": self.supplier,
                "company": "Waterman",
                "due_date": "2018-12-30",
                "currency": "UGX",
                "conversion_rate": 1.0,
                'buying_price_list': 'Standard Buying',
                'price_list_currency': 'UGX',
                'plc_conversion_rate': 1.0,
                'naming_series': 'PINV-',
                'base_net_total':0,
                'base_grand_total':0,
                'base_total':0,
                'grand_total':0,
                'credit_to': "2110 - Creditors - W",
                #"submit_on_creation": 1,
                "docstatus": 1,
                'items':[
                {
                    'qty': self.lessthree_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '0-3kgs',
                    'description': "Small",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.four_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '4kgs',
                    'description': "4Kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.fivetonine_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '5-9kgs',
                    'description': "5-9kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.tenfourteen_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '10-14kgs',
                    'description': "10-14kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.fifteennineteen_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '15-19kgs',
                    'description': "15-19kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.twentytonine_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '20-29kgs',
                    'description': "20-29kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.thirtytonine_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '30-39kgs',
                    'description': "30-39kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.fourtytonine_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '40-49kgs',
                    'description': "40-49kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                },{
                    'qty': self.morefifty_kgs,
                    'doctype':'Purchase Invoice Item',
                    'item_name': '50-100kgs',
                    'description': "50-100kgs",
                    'uom':'Kg',
                    'conversion_factor': 1.0,
                    #'rate': fee.unit_price,
                    'cost_center': 'Main - W'
                }
                ]
                }
        print(invoice_doc)
        #try:
        simplejson.loads(invoice_doc)
        client.insert(invoice_doc)
        #except:
        traceback.print_exc()

    pass

I dont know what im doing wrong.

fwiw I stumbled on this just now that may help Frappe Client: Need help using Submit() - #9 by tmatteson