Copy A Field From Parent Form to the Child

Hi all,

I am looking for a Custom Script, to copy a particular field from the Parent Form into the Child Form.

For eg,
In Doctype,
Delivery Note (Parent) there is a field A, and Delivery Note Item (Child) has a field B, When I put data in field A in the parent table, The data should be copied into Field B in the child table.

Thanks in Advance

@bibinqcs
You can try below code.

frappe.ui.form.on("Delivery Note","a", function(){
 for (var i =0; i < cur_frm.doc.items.length; i++){
 cur_frm.doc.items[x].b = cur_frm.doc.a //b is your child table field name.
}
cur_frm.refresh_field('items')
});

@mainul Thank you for the quick response, I will test it out and let you know how it goes.

Hi @mainul

I tried this, it did not work, I used in both Delivery Note and Delivery Note Item,

frappe.ui.form.on("Delivery Note","test_1", function(){ for (var i =0; i < cur_frm.doc.items.length; i++){ cur_frm.doc.items[x].d_date = cur_frm.doc.test_1 } cur_frm.refresh_field('items') }); but when I put an underscore as shown below, It makes the field test_1 in delivery note go blank once I add an item into the delivery note. frappe.ui.form.on("Delivery_Note","test_1", function(){ for (var i =0; i < cur_frm.doc.items.length; i++){ cur_frm.doc.items[x].d_date = cur_frm.doc.test_1 } cur_frm.refresh_field('items') }); What am I doing wrong?

Thanks for your help

@bibinqcs Sorry, It’s my mistake.

you just add only Delivery Note js file

cur_frm.doc.items[x].d_dat
not x its i.
below is the right code .

frappe.ui.form.on("Delivery Note","test_1", function(){
for (var i =0; i < cur_frm.doc.items.length; i++){
cur_frm.doc.items[i].d_date = cur_frm.doc.test_1 
}
cur_frm.refresh_field('items')
});
3 Likes

@mainul No Worries, Thanks for the help, I will check it out and get back to you :smiley:

@mainul Thanks a lot it works, This is going to be very helpful me.

While using this script I find there is a problem when multiple items were added while using the above code. It only copies the value into first Item. After I changed the condition to Validate it solves that issue.

I am posting it here so it can help someone else :smiley:

frappe.ui.form.on("Delivery Note","validate", function(){ for (var i =0; i < cur_frm.doc.items.length; i++){ cur_frm.doc.items[i].d_date = cur_frm.doc.test_1 } cur_frm.refresh_field('items') });
2 Likes

I am having same issue
trying to set cost_center in sales invoice item grid via a custom field cost_center on sales invoice.
frappe.ui.form.on(“Sales Invoice”,“validate”,function() {
var row = frappe.get_doc(cdt, cdn);
if(doc.cost_center) {
row.cost_center = doc.cost_center;
refresh_field(“cost_center”, cdn, “items”);
} else {
this.frm.script_manager.copy_from_first_row(“items”, row, [“cost_center”]);
}
}
);

any idea where I am making mistake??

The cost center field has to copy into each row

you would need this line in there as well

for (var i =0; i < cur_frm.doc.items.length; i++){
cur_frm.doc.items[i].cost_center = cur_frm.doc.cost_center
}

To work better you can use refresh instead of validate

frappe.ui.form.on(“Sales Invoice”,“refresh”, function(){
for (var i =0; i < cur_frm.doc.items.length; i++){
cur_frm.doc.items[i].cost_center = cur_frm.doc.cost_center
}
cur_frm.refresh_field(‘items’)
});

1 Like

thanks :grinning: