How can we add child table dynamically in frappe (ERPNEXT)
Asked Answered
D

4

6

I want to add child tables dynamically depending on records in another doctype.

Dm answered 11/4, 2019 at 11:23 Comment(1)
Please Post Question in detailInerney
G
7

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)

`

Gentille answered 3/5, 2019 at 16:16 Comment(0)
D
5

@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.

Deltoro answered 12/4, 2019 at 9:56 Comment(2)
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 dynamicallyDm
@Sajid Ijaz You can not add the fields runtime, instead, add the HTML field and render the html table with the dataDeltoro
E
2

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'
})
Eggleston answered 18/10, 2020 at 8:27 Comment(0)
H
1
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()
Hawfinch answered 22/11, 2021 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.