I want to add child tables dynamically depending on records in another doctype.
How can we add child table dynamically in frappe (ERPNEXT)
There are several method to add child in parent doc:
Method 1:
`
import frappe
parent = frappe.get_doc('Sales Order', 'SO-00002')
child = frappe.new_doc("Sales Order Item")
child.update({
'company': 'company_name',
'item_code': 'item_code',
'item_name': 'item_name',
'field': 'field_value'
'parent': parent.name,
'parenttype': 'Sales Order',
'parentfield': 'items'
})
parent.items.append(child)
Method 2:
import frappe
parent = frappe.get_doc('Sales Order', 'SO-00002')
child = frappe._dict({
'company': 'company_name',
'item_code': 'item_code',
'item_name': 'item_name',
'field': 'field_value'
})
parent.items.append(child)
`
@Sajid liaz,
You can add the row in the child table using the append method
e.g.
doc = frappe.get_doc('Sales Order', 'SO-00002')
doc.append('items', {
'company': 'company_name',
'item_code': 'item_code',
'item_name': 'item_name',
'field': 'field_value'
})
doc.save()
where items
is the child table's fieldname.
I want to add child tables dynamically not the data. for example if appraisal template has a child table with three rows then in appraisal it should generate three tables dynamically –
Dm
@Sajid Ijaz You can not add the fields runtime, instead, add the HTML field and render the html table with the data –
Deltoro
This is possible since DocTypes are treated as data in Frappe Framework. However, dynamic fields added at runtime must be added as Custom Fields.
from frappe.custom.doctype.custom_field.custom_field import create_custom_field
create_custom_field('Task' {
"fieldname": 'values',
"label": 'Values',
"fieldtype": 'Table',
"options": 'Child Table'
})
parent = frappe.get_doc('Sales Order', 'SO-00002')
parent.append("items", {
'company': 'company_name',
'item_code': 'item_code',
'item_name': 'item_name'
})
parent.save()
frappe.db.commit()
© 2022 - 2024 — McMap. All rights reserved.