How to fetch from master child table

hi
the master doctype (materials) have a child table (location_and_price) with two fields ( location and price)
In another doctype (cbd) I have a link field ( material ) linked to the doc type (materials)
and also another two fields ( location and price) with field type “select”
so now when I select material on cbd the two fields should automatically populate form the materials chlid table
i tried

cur_frm.add_fetch('materials','location_and_price.location','location')

but didnt work

thanks

please help

Try cur_frm.add_fetch('material', 'location', 'location')

i tried
cur_frm.add_fetch('material_name', 's_location', 'd_location')
but the source field “s_location” is found in a child table called “LocationAndPirce” doctype

it returns Server Error:
OperationalError: (1054, "Unknown column 's_location' in 'field list'")

It does not fetch from child table of materials. As material_name is linked to ‘materials’ table, add_fetch only fetches value from parent table.

@nabinhait So how to fetch child table values? Is there another function?

I am now in a similar situation , how to fetch values from a child table ?

Could anyone guide me please .

Thanks

I tried this script but I am getting an error

cur_frm.add_fetch('bom_no','item_code','new_item');

where bom_no is the link field

item_code is in a child table bom item in bom

and new_item is the destination field .

Error message

Traceback (most recent call last):
  File "/home/ommi/frappe-bench/apps/frappe/frappe/app.py", line 55, in application
    response = frappe.handler.handle()
  File "/home/ommi/frappe-bench/apps/frappe/frappe/handler.py", line 19, in handle
    execute_cmd(cmd)
  File "/home/ommi/frappe-bench/apps/frappe/frappe/handler.py", line 36, in execute_cmd
    ret = frappe.call(method, **frappe.form_dict)
  File "/home/ommi/frappe-bench/apps/frappe/frappe/__init__.py", line 876, in call
    return fn(*args, **newargs)
  File "/home/ommi/frappe-bench/apps/frappe/frappe/desk/form/utils.py", line 44, in validate_link
    % (fetch, frappe.db.escape(options), '%s'), (value,))[0]]
  File "/home/ommi/frappe-bench/apps/frappe/frappe/database.py", line 137, in sql
    self._cursor.execute(query, values)
  File "/home/ommi/frappe-bench/env/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/ommi/frappe-bench/env/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1054, "Unknown column 'item_code' in 'field list'") 

Please help

Thanks

Guys any help ?

Thanks

add_fetch works in following scenario:

  1. source field must be a linked doctype parent field
  2. link field and target field must be part of the same doctype. If link field is in parent form, target field also should be in parent form, otherwise both fields should be part of the same child table.

In the above case, as bom_no is a field of parent and item code is in child table, it should nto work. You have to write some other custom scripts for it.

1 Like

@nabinhait . Thanks for the reply could you guide me with the sample script please

Thanks

Hi @nabinhait

I tried this too but there is nothing showing. The field type is both currency.

frappe.ui.form.on(“Production Order”, {
refresh: function(frm) {
frm.add_fetch(“bom_no”, “raw_material_cost”, “raw_material_cost”)
}
});

raw_material_cost is a parent field in BOM DocType so I am unsure why it is not showing up in my Production Order DocType.

Not sure how to merge but this will help …

did you find solution?