Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 341
Sajid Ijaz Avatar asked Jan 26 '23 11:01

Sajid Ijaz


2 Answers

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

like image 162
Makarand Bauskar Avatar answered Feb 11 '23 04:02

Makarand Bauskar


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)

`

like image 24
Nav Avatar answered Feb 11 '23 04:02

Nav