Custom script - fetch data

Hello,

I applied this custom script to the doctype “Quotation Item”

cur_frm.add_fetch(‘item’,‘nombre_de_venta’,‘nombre_de_venta’)

My goal is to fetch the data from a custom field in the item profile whenever that item is added to the quotation.

I created the custom fields in both doctypes. In the item doctype the field is a data type field. In the quotation item doctype it is a read only type field. Under options I wrote item.nombre_de_venta.

However, it’s not working. What am I missing?

thanks
Sofia

Hello @sofia, add_fetch is joking with you, because it dont work for grid rows :smile:

you can use

frappe.ui.form.on('Quotation Item', 'item_code', function(frm, cdt, cdn){
    frappe.call({
        'method': 'frappe.client.get_value',
        'args': {
            'doctype': 'Item',
            'filters': [
                ['Item', 'item_code', '=', locals[cdt][cdn].item_code]
            ],
           'fieldname':'nombre_de_venta'
        },
        'callback': function(res){
            frappe.mode.set_value(cdt, cdn, 'nombre_de_venta', res.message.nombre_de_venta);
        }
    });
});
1 Like

@max_morais_dmm add_fetch does work for child tables!

Try item_code instead of item

cur_frm.add_fetch('item_code','nombre_de_venta','nombre_de_venta')
3 Likes

@rushabh_mehta, did you fixed it? because it was not working for a long time, since the version 5 layout release, many users have reported difficulties in the developers forum

@rmehta cur_frm.add_fetch ‘item_code’,‘nombre_de_venta’,‘nombre_de_venta’ worked! thank you!

For anyone else reading, make sure you go to the custom field that is ready only and check that what you have written in the options box matches your custom script.

I changed what I had written in my options field of the custom field I made in my quotation item doctype. It used to say item.nombre_de_venta and now it says item_code.nombre_de_venta.

2 Likes

Hi @rmehta,

I’m having the same issue with the supplier product number. I’d like the supplier product number to appear in the purchase invoice. Do I pull it from item_code, item_supplier, supplier? None of these are working for me.

thanks,
Sofia

1 Like

The format is:

add_fetch("link fieldname", "source fieldname", "target fieldname")
3 Likes

@rmehta,

The format is not working for this particular situation.

I have tried
cur_frm.add_fetch(‘supplier’,‘supplier_part_no’,‘supplier_part_number’) , which generates an error message and

cur_frm.add_fetch(‘item_supplier’,‘supplier_part_no’,‘supplier_part_number’), but this also does not work.

Because the Item Supplier doctype allows for a list of suppliers and supplier part numbers, I think Next does not know which supplier part number to select. Somehow I need to tell Next that it needs to select the supplier part number that belongs to the supplier selected for the purchase.

What is your item supplier fieldname?

source doctype = item supplier
fieldname that defines the source fieldname = supplier
source fieldname = supplier_part_no
target doctype = purchase invoice item
target fieldname = supplier_part_number

Not sure what is wrong. supplier_part_no should be in the Supplier doctype main table (not child table)

When I wrote the custom script “cur_frm.add_fetch(‘supplier’,‘supplier_part_no’,‘supplier_part_number’)” and then tried to make a purchase invoice, I got this message:

@sofia, have you made it work for this item supplier? Can you share? Tks

@jof2jc, no I haven’t. I started looking at other things but I’ll try again with the updated version, maybe the result will be different.

Thx @sofia. Would be great if anybody know how to fetch child table by other field trigger …

Hi ,
I added another script line with add_fetch after the first one and a very similar operational error message
while testing the automatic filled on selection of Item_code

Traceback (innermost last):
File “/home/frappe/press/benches/1511060614/apps/frappe/frappe/app.py”, line 67, in application
response = frappe.handler.handle()
File “/home/frappe/press/benches/1511060614/apps/frappe/frappe/handler.py”, line 75, in handle
execute_cmd(cmd)
File “/home/frappe/press/benches/1511060614/apps/frappe/frappe/handler.py”, line 109, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/frappe/press/benches/1511060614/apps/frappe/frappe/init.py”, line 777, in call
return fn(*args, **newargs)
File “/home/frappe/press/benches/1511060614/apps/frappe/frappe/desk/form/utils.py”, line 56, in validate_link
% (fetch, options, ‘%s’), (value,))[0]]
File “/home/frappe/press/benches/1511060614/apps/frappe/frappe/database.py”, line 135, in sql
self._cursor.execute(query, values)
File “/home/frappe/press/benches/1511060614/env/lib/python2.7/site-packages/MySQLdb/cursors.py”, line 205, in execute
self.errorhandler(self, exc, value)
File “/home/frappe/press/benches/1511060614/env/lib/python2.7/site-packages/MySQLdb/connections.py”, line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1054, “Unknown column ‘supplier_name’ in ‘field list’”)

What did i do wrong ?
Here are my two lines :
cur_frm.add_fetch(“item_code”, “item_name”, “item_name”); // [this one works fine]
cur_frm.add_fetch(“item_code”, “supplier_name”, “supplier_name”); //[adding this one generate error]

@Manu, Unknown column means too that Column dont exists, when do you add_fetch in a link docfield, frappe will look for the the table linked in the field, in this case Item and except if you have added in the Item a field named supplier_name it dont exists by default!

Ok, so, if i understand properly, that means i can’t use the same “linked field” to retrieve other data (like supplier_name, item_description , etc…) for my child table using an add_fetch line?
If that’s the case, how can i have multiple fields automatically updated based only a selected “linked field” then ?

@Manu, I never say it! What I say is! You can use any number of add_fetch statement in your code, since the lookup field already exists in the linked doctype

When do you add a add_fetch in item_code, item_code makes a reference for a field in the Item (assuming that item_code is linked with Item DocType), your universe of possibilities for fields to fetch, is determined by the linked DocType, in this case the doctype Item, if you take a look into the Item DocType, you will see that:

In the Supplier Details section, the field that make a reference to the supplier is named default_supplier, and dont have another field explicitely named supplier_name

Uploading download (4).png…

Now, if you need get the Supplier Name from the Supplier DocType, passing by reference the default_supplier from the Item doctype, dont have a easy way of do it in the client side!

You need get first the supplier name, and after do a call to fetch the value from the Supplier Doctype, how I have mentioned in the 2 message in this thread!

2 Likes

I got it :smiley:
Million thx for your help