Need guidance to auto populate child table

Hi All ,

Please someone let me know how to auto populate the values in the child table when something is entered in the parent. If it is a normal field I would enter the below code and it works but how to auto populate a child table when I enter something in parent ?

    {
    function compute(doc, cdt, cdn){
        if (doc.hammer1){ 
    doc.hammer = doc.hammer1;
    refresh_field("hammer");
        }}
    cur_frm.cscript.custom_hammer1 = compute;

    }

Thanks in advance

Check if this function from custom doctype worker_sheet.js helps,

calculate_totals = function(doc) {
	var attendance = doc.worker_attendance || [];
	doc.total_wages = 0.0;
	doc.outstanding_wages = 0.0;
	doc.daily_wages = 0.0;
	for(var i=0;i<attendance.length;i++) {
		if (attendance[i].is_daily_paid === 1){
			if (!attendance[i].hours){
				attendance[i].hours = doc.working_hours;
			}
			daily_wages = flt(flt(attendance[i].rate) * flt(attendance[i].hours), 2);
			doc.daily_wages += daily_wages;
		}
		total_wages = flt(flt(attendance[i].rate) * flt(attendance[i].hours), 2);
		doc.total_wages += total_wages;
	}
	doc.outstanding_wages = flt(flt(doc.total_wages) - flt(doc.daily_wages));
	refresh_field('total_wages');
	refresh_field('daily_wages');
	refresh_field('outstanding_wages');
}

It loops through worker_attendance table, and sets attendence[i].hours to value from parent doc.working_hours if no value is set in child field for hours

1 Like

Many thanks for your swift response @revant_one sir , I followed your example and modified my code and pasted in both parent and child table location but it is not reflecting , I am trying to pull the value of cmill which is in parent to the field cmill1 which is in child .Please guide me

{
function compute(doc, cdt, cdn){
if (doc.cmill){ 
var cmill1 = doc.cmill;
refresh_field("cmill1");
}}
cur_frm.cscript.custom_cmill = compute;

} 

Thanks in advance

fields considered in code

doc.cmill this is in parent doc
doc.table_field (this is the child table field)
table_field[i].cmill1 this is in child table

calculate_totals = function(doc) {
	var table = doc.table_field || [];
	for(var i=0;i<table.length;i++) {
		table[i].cmill1 = doc.cmill;
	}
	refresh_field('table_field');
}
1 Like

Thanks a ton for the help sir @revant_one , but I could not see it working , please help

This is what I did

calculate_totals = function(doc) {
	var table = doc.mill || [];
	for(var i=0;i<table.length;i++) {
		table[i].cmill1 = doc.cmill;
	}
	refresh_field('mill');
}

cmill field in parent
cmill1 field in child
table name mill
table field name mill

But I could not see anything getting updated , please help

@revant_one Sir even I tried this it is not working .please help

 frappe.ui.form.on("Item", {
item: function(frm) {          
frappe.call({                        
                     method: "frappe.client.get_value", 
                     args: { 
                            doctype: "Item",
			        fieldname: "cmill",
			        filters: { name: frm.doc.item},
                     },
                     callback: function(r) {
                             {
                                      frappe.model.set_value(cdt, cdn, "cmill1", r.message.cmill);
                             }
                             
                     }
            });
}});