Programmatically add Journal Entry

I am testing adding Journal Entry programmatically. This is my sample code.

    def on_submit(self):
        from erpnext.accounts.general_ledger import make_gl_entries
        gl_map = []

        credit_account = frappe.get_doc("Account", "Debtors - CI")
        debit_account = frappe.get_doc("Account", "Cash - CI")
        customer = frappe.get_doc("Customer", "Juan de la cruz")

        gl_map.append(
            frappe._dict({
                "account": credit_account,
                "party_type": "Customer",
                "party": customer,
                "against": debit_account,
                "debit": flt(10000),
                "credit": flt(10000),
                "remarks": "Test Journal",
            })
        )

        if gl_map:
            make_gl_entries(gl_map, cancel=0, adv_adj=0)

but when I submit I got this error

Incorrect number of General Ledger Entries found. You might have selected a wrong Account in the transaction.

What did I miss? Or I did it totally wrong?

1 Like

I have solve this problem. You need to have 2 or more entries in GL. Code below

        from erpnext.accounts.general_ledger import make_gl_entries
        gl_map = []

        gl_map.append(
            self.get_gl_dict({
                "account": "Debtors - CI",
                "party_type": "Customer",
                "party": "Juan de la cruz",
                "credit": flt(10000),
                "debit": flt(0),
                "remarks": "Test Journal"
            }))

        gl_map.append(
            self.get_gl_dict({
                "account": "Cash - CI",
                "credit": flt(0),
                "debit": flt(10000),
                "remarks": "Test Journal"
            }))
4 Likes

@ccfiel good work!

ERPNext use the principle of Double Entry Bookkeeping, that is the basis of accounting in all the world!

Just to share a small learn reference https://en.wikipedia.org/wiki/Double-entry_bookkeeping_system

2 Likes

@ccfiel just trying inserting a new Journal Entry

frappe.get_doc({
  "doctype": "Journal Entry",
  "posting_date": "2015-01-01",
  "accounts": [{
    "account": ...
  }],
  ...
}).insert()
2 Likes

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.