[Solved] Insert and Submit documents with child tables in python script

Hi,

I have an external data source for Journal Entries. I am trying to get a python script in a custom app to read in this data, insert it into the Journal Entry table, and submit them. I’ve gotten as far as this (jeac variables are Journal Entry Account - credit, jead variables are Journal Entry Account - debit and je variables are Journal Entries).

It’s failing with the error “frappe.exceptions.ValidationError: Accounts table cannot be blank.” for a good reason: I can’t figure out how to attach accounts to the journal entry inside the script. Can anyone help?

                    jead_values_dict = {'name':jead_name,
                                        'creation':jead_creation,
                                        'modified':jead_modified,
                                        'modified_by':jead_modified_by,
                                        'owner':jead_owner,
                                        'docstatus':jead_docstatus,
                                        'parent':jead_parent,
                                        'parentfield':jead_parentfield,
                                        'parenttype':jead_parenttype,
                                        'idx':jead_idx,
                                        'account':jead_account,
                                        'credit':jead_credit,
                                        'party_balance':jead_party_balance,
                                        'against_account':jead_against_account,
                                        'is_advance':jead_is_advance,
                                        'debit':jead_debit,
                                        'debit_in_account_currency':jead_debit_in_account_currency,
                                        'cost_center':jead_cost_center,
                                        'balance':jead_balance,
                                        'exchange_rate':jead_exchange_rate,
                                        'account_type':jead_account_type,
                                        'debit_in_account':jead_debit_in_account,
                                        'currency':jead_currency,
                                        'account_currency':jead_account_currency,
                                        'credit_in_account_currency':jead_credit_in_account_currency,
                                        'company':jead_company}
                    jeac_values_dict = {'name':jeac_name,
                                        'creation':jeac_creation,
                                        'modified':jeac_modified,
                                        'modified_by':jeac_modified_by,
                                        'owner':jeac_owner,
                                        'docstatus':jeac_docstatus,
                                        'parent':jeac_parent,
                                        'parentfield':jeac_parentfield,
                                        'parenttype':jeac_parenttype,
                                        'idx':jeac_idx,
                                        'account':jeac_account,
                                        'credit':jeac_credit,
                                        'party_balance':jeac_party_balance,
                                        'against_account':jeac_against_account,
                                        'is_advance':jeac_is_advance,
                                        'debit':jeac_debit,
                                        'debit_in_account_currency':jeac_debit_in_account_currency,
                                        'cost_center':jeac_cost_center,
                                        'balance':jeac_balance,
                                        'exchange_rate':jeac_exchange_rate,
                                        'account_type':jeac_account_type,
                                        'debit_in_account':jeac_debit_in_account,
                                        'currency':jeac_currency,
                                        'account_currency':jeac_account_currency,
                                        'credit_in_account_currency':jeac_credit_in_account_currency,
                                        'company':jead_company}
                    je_values_dict = {"doctype":"Journal Entry",
                                      "name": je_name,
                                      "creation":je_creation,
                                      "modified":je_modified,
                                      "cheque_date":je_cheque_date,
                                      "modified_by":je_modified_by,
                                      "owner":je_owner,
                                      "docstatus":je_docstatus,
                                      "idx":je_idx,
                                      "write_off_amount":je_write_off_amount,
                                      "naming_series":je_naming_series,
                                      "voucher_type":je_voucher_type,
                                      "letter_head":je_letter_head,
                                      "cheque_no":je_cheque_no,
                                      "bill_no":je_bill_no,
                                      "difference":je_difference,
                                      "title":je_title,
                                      "total_amount_in_words":je_total_amount_in_words,
                                      "company":je_company,
                                      "fiscal_year":je_fiscal_year,
                                      "total_credit":je_total_credit,
                                      "user_remark":je_user_remark,
                                      "remark":je_remark,
                                      "total_amount":je_total_amount,
                                      "write_off_based_on":je_write_off_based_on,
                                      "total_debit":je_total_debit,
                                      "is_opening":je_is_opening,
                                      "posting_date":je_posting_date,
                                      "multi_currency":je_multi_currency,
                                      "budgeting_account":je_budgeting_account}
                    record = frappe.get_doc(je_values_dict)
                    #record.append("accounts", {jeac_values_dict,jead_values_dict}) - Fails due to dict hashing error. Not sure how else to do this.
                    record.insert()
                    record.submit()

I fixed my own problem! Yay!

My problems were threefold:

  • I didn’t realize that I couldn’t just pass the JEA dictionaries into it and get it to work, I need to create the child document first
  • I need to append them one by one (due to that dictionary/doctype hashing issue).
  • Need to set ‘doctype’: ‘Journal Entry Account’ in the JEA value dictionaries
    Last few lines (working) look like this:

jeac_record = frappe.get_doc(jeac_values_dict) jead_record = frappe.get_doc(jead_values_dict) jeac_record.insert() jead_record.insert() record = frappe.get_doc(je_values_dict) record.append("accounts", jeac_record) record.append("accounts", jead_record) record.insert() record.submit()

I guess this was useful for being my rubber duck.

Inserting child records first is not a good idea.

Instead you should try following:

record = frappe.get_doc(je_values_dict)
record.append("accounts", jeac_record)
record.append("accounts", jead_record)
record.insert()
record.submit()

while creating any entry, there is no need to specify following details:
name, creation, modified, modified_by, owner, docstatus, parent, parentfield and parenttype .
These fields are set automatically.

2 Likes

If solved, please request the topic to be closed :slight_smile:

@nabinhait Ah, ok. Thanks.

@Marcin_Walkow Certainly, I think this can be closed.

1 Like