Frappe.get_doc, error on submit() not able to insert childtable

Error While trying to Submit “Payment Entry” on submit “Sales Invoice” .

### App Versions

{
“erpnext”: “15.0.0-dev”,
“frappe”: “15.0.0-dev”,
“frappe_whatsapp”: “0.0.2”
}

### Route

Form/Sales Invoice/ACC-SINV-2023-00075

### Traceback

Traceback (most recent call last):
File “apps/frappe/frappe/app.py”, line 83, in application
response = frappe.api.handle()
File “apps/frappe/frappe/api.py”, line 53, in handle
return _RESTAPIHandler(call, doctype, name).get_response()
File “apps/frappe/frappe/api.py”, line 69, in get_response
return self.handle_method()
File “apps/frappe/frappe/api.py”, line 79, in handle_method
return frappe.handler.handle()
File “apps/frappe/frappe/handler.py”, line 48, in handle
data = execute_cmd(cmd)
File “apps/frappe/frappe/handler.py”, line 86, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “apps/frappe/frappe/init.py”, line 1650, in call
return fn(*args, **newargs)
File “apps/frappe/frappe/utils/typing_validations.py”, line 30, in wrapper
return func(*args, **kwargs)
File “apps/frappe/frappe/desk/form/save.py”, line 34, in savedocs
doc.submit()
File “apps/frappe/frappe/model/document.py”, line 1028, 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 326, in save
return self._save(*args, **kwargs)
File “apps/frappe/frappe/model/document.py”, line 378, in _save
self.run_post_save_methods()
File “apps/frappe/frappe/model/document.py”, line 1113, in run_post_save_methods
self.run_method(“on_submit”)
File “apps/frappe/frappe/model/document.py”, line 941, 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 154, in execute_doc
safe_exec(self.script, _locals={“doc”: doc}, restrict_commit_rollback=True)
File “apps/frappe/frappe/utils/safe_exec.py”, line 81, in safe_exec
exec(
File “”, line 34, in
File “apps/frappe/frappe/model/document.py”, line 1028, 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 326, in save
return self._save(*args, **kwargs)
File “apps/frappe/frappe/model/document.py”, line 378, in _save
self.run_post_save_methods()
File “apps/frappe/frappe/model/document.py”, line 1113, in run_post_save_methods
self.run_method(“on_submit”)
File “apps/frappe/frappe/model/document.py”, line 937, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File “apps/frappe/frappe/model/document.py”, line 1308, in composer
return composed(self, method, *args, **kwargs)
File “apps/frappe/frappe/model/document.py”, line 1290, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File “apps/frappe/frappe/model/document.py”, line 934, in fn
return method_object(*args, **kwargs)
File “apps/erpnext/erpnext/accounts/doctype/payment_entry/payment_entry.py”, line 100, in on_submit
self.make_gl_entries()
File “apps/erpnext/erpnext/accounts/doctype/payment_entry/payment_entry.py”, line 1046, in make_gl_entries
gl_entries = self.build_gl_map()
File “apps/erpnext/erpnext/accounts/doctype/payment_entry/payment_entry.py”, line 1040, in build_gl_map
self.add_bank_gl_entries(gl_entries)
File “apps/erpnext/erpnext/accounts/doctype/payment_entry/payment_entry.py”, line 1210, in add_bank_gl_entries
self.get_gl_dict(
File “apps/erpnext/erpnext/controllers/accounts_controller.py”, line 785, in get_gl_dict
account_currency = get_account_currency(gl_dict.account)
File “apps/erpnext/erpnext/accounts/doctype/account/account.py”, line 369, in get_account_currency
return frappe.local_cache(“account_currency”, account, generator)
File “apps/frappe/frappe/init.py”, line 2168, in local_cache
local.cache[namespace][key] = generator()
File “apps/erpnext/erpnext/accounts/doctype/account/account.py”, line 361, in generator
account_currency, company = frappe.get_cached_value(
TypeError: cannot unpack non-iterable NoneType object

### Request Data

{
“type”: “POST”,
“args”: {
“doc”: “{"name":"ACC-SINV-2023-00075","owner":"Administrator","creation":"2023-12-22 18:18:52.809814","modified":"2023-12-22 18:45:42.876978","modified_by":"Administrator","docstatus":0,"idx":0,"title":"Ahmed Moosa","naming_series":"ACC-SINV-.YYYY.-","customer":"Abideen","customer_name":"Ahmed Moosa","company":"Al Raqi","posting_date":"2023-12-22","posting_time":"19:48:40","set_posting_time":1,"due_date":"2023-12-22","is_pos":0,"posa_is_printed":0,"is_consolidated":0,"is_return":0,"update_billed_amount_in_sales_order":0,"is_debit_note":0,"cost_center":"Main - AR","currency":"OMR","conversion_rate":1,"selling_price_list":"Standard Selling","price_list_currency":"OMR","plc_conversion_rate":1,"ignore_pricing_rule":0,"update_stock":0,"total_qty":1,"total_net_weight":0,"base_total":5,"base_net_total":5,"total":5,"net_total":5,"tax_category":"","base_total_taxes_and_charges":0,"total_taxes_and_charges":0,"base_grand_total":5,"base_rounding_adjustment":0,"base_rounded_total":0,"base_in_words":"","grand_total":5,"rounding_adjustment":0,"use_company_roundoff_cost_center":0,"rounded_total":0,"in_words":"","total_advance":0,"custom_advance_paid":100,"outstanding_amount":5,"disable_rounded_total":1,"apply_discount_on":"Grand Total","base_discount_amount":0,"is_cash_or_non_trade_discount":0,"additional_discount_percentage":0,"discount_amount":0,"total_billing_hours":0,"total_billing_amount":0,"custom_length":50,"custom_wrist":50,"custom_neck":50,"custom_pocket_type_1":"Angular","custom_arm_up_loose":50,"custom_shoulder_shape":"Designer","custom_chest_size":50,"custom_shoulder":50,"custom_side_loose":50,"custom_front_open":"Yes","custom_outside_stiching":"Yes","custom_bottom_border":"Yes","custom_pocket_type":"Designer","custom_arm_length":50,"custom_bottom_loose":50,"custom_flower_length":0,"custom_in_side_stiching":0,"custom_body_shape":"Thin","custom_pocket_zig_zag":"Yes","custom_zigzag":"Yes","custom_design":"New","custom_thread_no":"21","custom_comments":"Use new thread","base_paid_amount":0,"paid_amount":0,"base_change_amount":0,"change_amount":0,"allocate_advances_automatically":0,"only_include_allocated_payments":0,"write_off_amount":0,"base_write_off_amount":0,"write_off_outstanding_amount_automatically":0,"redeem_loyalty_points":0,"loyalty_points":0,"loyalty_amount":0,"contact_person":"Abideen-Abideen","contact_display":"Abideen","contact_mobile":"95078855","contact_email":"","territory":"All Territories","ignore_default_payment_terms_template":0,"po_no":"","debit_to":"Debtors - AR","party_account_currency":"OMR","is_opening":"No","against_income_account":"Sales - AR","amount_eligible_for_commission":5,"commission_rate":0,"total_commission":0,"group_same_items":0,"language":"en","status":"Draft","customer_group":"All Customer Groups","is_internal_customer":0,"is_discounted":0,"remarks":"No Remarks","repost_required":0,"posa_delivery_charges_rate":0,"doctype":"Sales Invoice","timesheets":[],"taxes":[],"items":[{"name":"df9407be5c","owner":"Administrator","creation":"2023-12-22 18:18:52.809814","modified":"2023-12-22 18:45:42.876978","modified_by":"Administrator","docstatus":0,"idx":1,"has_item_scanned":0,"item_code":"001","item_name":"Musar","description":"Musar","item_group":"Products","image":"","qty":1,"stock_uom":"Nos","uom":"Nos","conversion_factor":1,"stock_qty":1,"price_list_rate":5,"base_price_list_rate":5,"margin_type":"","margin_rate_or_amount":0,"rate_with_margin":0,"discount_percentage":0,"discount_amount":0,"base_rate_with_margin":0,"rate":5,"amount":5,"base_rate":5,"base_amount":5,"pricing_rules":"","posa_offer_applied":0,"posa_is_offer":0,"stock_uom_rate":5,"is_free_item":0,"grant_commission":1,"net_rate":5,"net_amount":5,"base_net_rate":5,"base_net_amount":5,"delivered_by_supplier":0,"income_account":"Sales - AR","is_fixed_asset":0,"expense_account":"Cost of Goods Sold - AR","enable_deferred_revenue":0,"weight_per_unit":0,"total_weight":0,"warehouse":"Stores - AR","incoming_rate":2,"allow_zero_valuation_rate":0,"item_tax_rate":"{}","actual_batch_qty":0,"actual_qty":4,"delivered_qty":0,"cost_center":"Main - AR","page_break":0,"parent":"ACC-SINV-2023-00075","parentfield":"items","parenttype":"Sales Invoice","doctype":"Sales Invoice Item"}],"pricing_rules":[],"sales_team":[],"payment_schedule":[{"name":"1711658846","owner":"Administrator","creation":"2023-12-22 18:18:52.918148","modified":"2023-12-22 18:45:42.876978","modified_by":"Administrator","docstatus":0,"idx":1,"due_date":"2023-12-22","invoice_portion":100,"discount":0,"payment_amount":5,"outstanding":5,"paid_amount":0,"discounted_amount":0,"base_payment_amount":5,"parent":"ACC-SINV-2023-00075","parentfield":"payment_schedule","parenttype":"Sales Invoice","doctype":"Payment Schedule"}],"packed_items":[],"advances":[],"payments":[],"__onload":{"make_payment_via_journal_entry":0},"__last_sync_on":"2023-12-22T14:47:42.296Z"}”,
“action”: “Submit”
},
“btn”: {
“jQuery370053064287065613721”: {
“events”: {
“click”: [
{
“type”: “click”,
“origType”: “click”,
“guid”: 781,
“namespace”: “”
}
]
}
}
},
“freeze”: true,
“headers”: {},
“error_handlers”: {},
“url”: “/api/method/frappe.desk.form.save.savedocs”
}

### Response Data

{
“exc_type”: “DoesNotExistError”,
“exception”: “TypeError: cannot unpack non-iterable NoneType object”,
“_exc_source”: “Server Script”
}

While in another bench on another site its working fine.

here is my script

todo = frappe.get_doc({
    "doctype":"Payment Entry",
    "naming_series": "ACC-PAY-.YYYY.-",
    "payment_type": "Receive",
    "party_type": "Customer",
    "party": doc.customer,
    "paid_amount": doc.custom_advance_paid,
    "received_amount": doc.custom_advance_paid,
    "target_exchange_rate": "0",
    "paid_to": "Cash - US",
    "paid_to_account_currency": doc.currency
})
todo.insert()
todo.submit()

Since you mentioned that the script works fine on another bench and site, it’s possible that there might be some configuration or data-related differences between the two environments. You can compare the configurations and data between the working and non-working sites to identify any discrepancies.

1 Like

Thanks for reply.

Issue Solved,

Company Abbr for Account was wrong.

After correcting this Its working fine.

But now I am not able to create child table entries for table “references” in Payment Entry.

p = frappe.get_doc({
    "doctype":"Payment Entry",
    "naming_series": "ACC-PAY-.YYYY.-",
    "payment_type": "Receive",
    "party_type": "Customer",
    "party": doc.customer,
    "paid_amount": doc.custom_advance_paid,
    "received_amount": doc.custom_advance_paid,
    "target_exchange_rate": "0",
    "paid_to": "Cash - AR",
    "mode_of_payment": "Cash",
    "paid_to_account_currency": doc.currency,
    "references": [
        {
            "reference_doctype": "Sales Invoice",
            "reference_name": "ACC-SINV-2023-00063",
            "due_date": "2023-12-22",
            "account": "Debtors - AR"
        }
    ]
})
p.insert(ignore_permissions=True)
p.submit()
2 Likes

I am using System Console to understand the frappe.get_doc(), insert() and submit().

Please check below script written for Sales Order.

doc = frappe.get_doc({
    "doctype": "Sales Order",
    "customer": "Frappe Ash",
    "delivery_date": "2023-12-30",
    "items": [{
        "item_name": "123456",
        "qty": 5,
        "rate": 456
    }]
})

log(doc)
log(doc.items)
doc.insert()
log(doc)
log(doc.name)
log(doc.items)
doc.submit()
log(doc.status)
log(doc.items)

Output :

SalesOrder(unsaved)
[<SalesOrderItem: unsaved>]
SalesOrder(SAL-ORD-2023-00007)
SAL-ORD-2023-00007
[<SalesOrderItem: 611dc630a6 parent=SAL-ORD-2023-00007>]
To Deliver and Bill
[<SalesOrderItem: 611dc630a6 docstatus=1 parent=SAL-ORD-2023-00007>]

It seems Sales Order inserted and submitted successfully with child table.

Before and after doc.insert(), output of log(doc.items) which is child table is not empty.

Even after submit() its showing in output.

Now I am trying same method to insert and submit “Payment Entry”.

Script for insert and submit “Payment Entry”.

doc = frappe.get_doc({
    "doctype": "Payment Entry",
    "party_type": "Customer",
    "party": "Frappe Ash",
    "paid_amount": 4560,
    "received_amount": 4560,
    "target_exchange_rate": "0",
    "paid_to": "Cash - US",
    "paid_to_account_currency": "INR",
    "references": [{
        "reference_doctype": "Sales Invoice",
        "qreference_namety": "ACC-SINV-2023-00021"
    }]
})

log(doc)
log(doc.references)
doc.insert()
log(doc)
log(doc.name)
log(doc.references)
doc.submit()
log(doc.status)
log(doc.references)

Output :

EmployeePaymentEntry(unsaved)
[<PaymentEntryReference: unsaved>]
EmployeePaymentEntry(ACC-PAY-2023-00030)
ACC-PAY-2023-00030
[]
Submitted
[]

After insert() new doc is created, but log(doc.references) got empty.

I am not understanding why this child table is getting empty. :face_with_monocle:

How to resolve this? How to insert and submit child table in Payment Entry?

@ncp @avc

Hi @umarless:

Use append method to create child.
Something like this:

si = frappe.get_doc("Sales Invoice", your_sales_invoice)

doc = frappe.get_doc({
    "doctype": "Payment Entry",
    "party_type": "Customer",
    "party": si.customer
    "paid_amount": your_paid_amount,
    "received_amount": your_received_amount,
    "target_exchange_rate": "0",
    "paid_to": "Cash - US",
    "paid_to_account_currency": "INR"
    })
    
doc.append("references", 
    {
    	"reference_doctype": "Sales Invoice",
		"reference_name": si.name,
		"due_date": si.due_date,
		"total_amount": si.grand_total,
		"outstanding_amount": si.outstanding_amount,
		"allocated_amount": si.outstanding_amount,
	}
)

doc.insert().submit()

Hope this helps.

2 Likes

Thanks a lot @avc for your reply.

I found that to insert a document “Payment Entry” with child table " Payment Entry References" we need to add the field “allocated_amount” compulsorily. (it’s not mandatory in form), without adding this field in script, child table is not getting insert(or inserting empty).

System Console script and output without field “allocated_amount” :point_down:

System Console script and output with field “allocated_amount” :point_down:

Normally, After selecting “Type” and “Name” in table “Payment References”, fields “Grand Total”, “Outstanding” and “Allocated” gets fetch. :point_down:

Working script to create Payment Entry after submitting Sales Invoice.

Documenting it just for future reference. :grimacing:

Thanks.

1 Like

Hi @umarless:

Maybe allocated_amount is needed just for some cases, mandatory property wouldn’t makes sense there.

There are a lot of logic business validations and methods on payment_entry controller …
Thanks for clarify for other people.

1 Like