Creating or updating document with children

Greetings everyone,
I am trying to create a custom doc(tool) that will allow me to bulk create or update a document.
however, when i click the execute button, I get no prompt and the new record is not save even though the record name is returned. Also when the record is existing and the child is not, the child is not added or saved.

sample code:
def make_doc(dt,dn):
customdoc = frappe.get_doc(dt, dn)

for emp in customdoc.employees:
new_doc = 0
employee_doc = frappe.get_all(“doc”, filters={‘status’: ‘draft’,“employee”:emp.employee}, fields=["*"])

time_log_ctr = 0
if not employee_doc:
  new_doc = 1
  doc = frappe.new_doc("doc")
  doc.company = doc.company
  doc.status = "draft"
  doc.employee = emp.employee
  doc.start_date = doc.for_date

  if not doc:
    frappe.throw(_("Unable to create doc "))
elif employee_doc:
  doc = frappe.get_doc("doc",employee_doc[0].name)
  doc.doc_childs = frappe.get_all("doc Detail", filters={'parent': doc.name}, fields=["*"])
  doc_childs = doc.doc_childs
  
a_doc_child = doc.get_doc_child(doc.name,doc.var1,emp.varaa)
if not a_doc_child and emp.varaa > 00:
  doc.append("doc_childs",{"var1":1,"varb":emp.varaa})


b_doc_child = doc.get_doc_child(doc.name,doc.var1,emp.varab)
if not b_doc_child and emp.varab > 00:
  doc.append("doc_childs",{"var1":1,"varb":emp.varab})


c_doc_child = doc.get_doc_child(doc.name,doc.var1,emp.varac)
if not c_doc_child and emp.varac > 00:
  doc.append("doc_childs",{"var1":1,"varb":emp.varac})

if new_doc == 1:
  doc.insert()

#else:
#  doc.insert()

by the way, I was able to create a new doc record with a child. It will work if I comment out the if-condition block and add “doc.append(“doc_childs”,{“var1”:1,“varb”:emp.varaa})” below the “doc.start_date” line.

Why not use Data Import Tool to bulk create documents ?
Data Import also provides the feature to update already existing doc and importing them back.

Moreover, you also get bulk edit option in listview

Hi Zlash65
thanks for the prompt reply. Yes the import tool could be useful. However the end user of the system might not be able to memorize all the ids and might mistype some of the data. Also some end users are not that tech-savvy. Especially if you have other work to do besides encoding in the system. Right now, Im just trying to learn on what I can do at the back-end and hopefully transform the code in to a useful tool.

Thanks

There’s no need to remember any ids right? If you use Data Import, you can directly download the template in excel format with the columns header pre-filled accordingly. A user would just need to add the data in the columns of the respective fields.

Also, Data Import provides facility to download the template with already existing data in the system. This should make the task easier for the user who can refer the existing data and create new rows in the excel for the data they want to enter.

Hi Zlash65
thanks…
Anyways, do you any idea why, the code above does not actually save the data?.
When I use msgprint to print the doc.name it will display the record name. However, I cannot seem to find the record being saved if I view the lists. Also there is no error prompt what so ever.

there are times that doc.insert() alone does not work. Im still not sure how.

but as mentioned in this discussion Cant insert new documents from python scripts

by doing the following:

doc.insert()
frappe.db.commit()

will save the data.