Passing field value from Parent to child doctype

Hi All,

I am trying to pass the value from the field named “Region” in parent doctype “Stationery Request” to the child doctype named “Item List” which show as table in the parent with the field

  1. Region (hidden)
  2. Location (show in list view)
  3. QTY (show in list view)
  4. Price (show in list view)

I already written the filter for the other fields based on the Region which populate data from another doctype.

Region must contain the same value as parent doctype so the filter would work please let me know how to pass the value for the one field.

Hello @balramrjpm

You can use the client script given below. This can solve your problem…

frappe.ui.form.on(‘Stationery Request’, {
refresh: function(frm) {
frm.doc.item_list.forEach(function(row) {
frappe.model.set_value(row.doctype, row.name, ‘region’, frm.doc.region);
});
},

region: function(frm) {
    frm.doc.item_list.forEach(function(row) {
        frappe.model.set_value(row.doctype, row.name, 'region', frm.doc.region);
    });
}

});

Hi thks for the reply. I tried which is not working as you mentioned.
but not working

Changed the named from the region to product line.

The product line appears in both parent and child doctype.

Whenever I add the row the field populate the row with parent doctype field value for the product line alone then the selection will proceed.

image

product_line: function(frm) {
    frm.doc.item_list.forEach(function(row) {
        frappe.model.set_value(row.doctype, row.name, 'product_line', frm.doc.product_line);
            });
        }

@balramrjpm check this Add rows with values in child table

Thks for the response. But I looking for creating a child table with 8 columns. Once user clicks the add row product line column need to appear by default and other columns need to populate based on the selection.

  1. Product Line - which depends on parent doctype
  2. Category - which need to populate with select datatype populate with filtering targeting the column " product line" as primary key and produce the category from another doctype named “Category”
  3. Sub Category - Once Category field selected then using the value from Category child field it need to populate the Sub category by filtering another doctype named Sub category… Same goes on for other fields

Pls advise.

yes, you can set it using the Child Table Events

When using the child table event and clicking on “add row”, the data will automatically populate based on the scenario.

items_add(frm, cdt, cdn) { // "items" is the name of the table field in XYZ DocType, "_add" is the event
    frappe.model.set_value(row.doctype, row.name, 'product_line', frm.doc.product_line);
}

please check your child table name and set it in the script according to the scenario.

Hi thks could u pls brief in where I need to define this function I am using the javascript and I don’t have the permission for the client script currently let me know whether it can be achieved thks in advance

without client script permission, how can you achieve the functionality?

you have to add the client script and i already provided the documentation, so please check it the example.

I m beginner level in frappe, I know but currently we are facing some issue with server so they restricted for now will receive in later stage.

I came with the alternative solution created the section with all the values with one button which could push the value to the table.

but here also I am facing issue.

Pls advise as the button not getting triggered second time

add_item: function(frm) {
    // Add a new row to the child table with values from the parent document
    
    
    var new_row = frm.add_child('item_list');
    new_row.category = frm.doc.category;
    new_row.sub_category = frm.doc.sub_category;

    // Add more fields as needed

    // Refresh the child table to show the new row
    frm.refresh_field('item_list');
    frm.refresh();
    
}

});

User frappe.model.set_value

set the script for child table

@NCP with earlier code I have completed the changes but I need use custom button instead of the default add row button

Hereby I attached the screenshot my scope, user want the button below instead of the table

pls advise

Thanks in advance

Check the reference and apply logic according to the scenario.

Thks for the response but I want this button to act as the same as add new in the child table i.e., Inserting the row in the child table whether it is capable or not as it didnt work for me.