How to get data from Child table to Doctype

Hi,

I would like to get data from Child table to Doctype like this :

From
Child Doctype : Purchase Receipt Item
Custom field : crop_type
To
Doctype : Batch
Custon field : crop_type

Here is the custom script I tried, but I don’t know why it is not working :

frappe.ui.form.on('Batch', {
    refresh(frm) {
        frappe.db.get_value('Purchase Receipt Item', {'parent': doc.reference_name, 'item_code': doc.item}, 'crop_type', (r) => {
            frm.set_value('crop_type', r.crop_type);
});
    }
});

Can someone help me?

Thank you!

I also tried this :

frappe.ui.form.on('Batch', {
    refresh(frm) {
        frappe.db.set_value("Crop Type", get_value('Purchase Receipt Item', {'parent': doc.reference_name,'item_code': doc.item }, 'crop_type'))
    }
});

And this

frappe.ui.form.on("Batch", function(frm) {
        frm.set_value("Crop Type", get_value('Purchase Receipt Item', {'parent': doc.reference_name,'item_code': doc.item }, 'crop_type'))
    }
);

And this

frappe.ui.form.on("Batch", "crop_type", function(frm) {
    frappe.db.get_value('Purchase Receipt Item', {'parent': doc.reference_name,'item_code': doc.item }, 'crop_type')
    .then(r => {
     let values = r.message;
        frm.set_value("crop_type", values.crop_type);
    });
});

And this

frappe.ui.form.on('Batch', {
    refresh: function(frm) {
        frappe.db.get_value('Purchase Receipt Item', {'parent': doc.reference_name, 'item_code': doc.item}, 'crop_type')
        .then(r => {
            frm.set_value('crop_type', r.crop_type);
        })}
});

But it is not working either. Can somebody help me ? :slight_smile:

Hi @FredericVerville,

I think, in js, it’s hard to implement for a child to parent fieldset.

Please apply the server script.

Script:

for d in doc.get('items'):
    batch_name = frappe.db.get_value("Batch", {'name': d.batch_no, 'item': d.item_code}, 'name')
    if batch_name:
        bn = frappe.get_doc("Batch", batch_name)
        bn.crop_type = d.crop_type
        bn.save()

Thank You!

1 Like

If you want to set the field value on batch creation, this may help you: