# Child Table Calculation

Is there any possibilities for above scenario ?

many thanks brother

if i want to add a 5th column on the table and make the field to be calculated by column1 * column2 what should i do?

This may help:

``````//The following three scripts calculates the rate/amount based on quantity.
frappe.ui.form.on("Material Request Item", {
rate: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, 'amount', (d.qty * d.rate));
var total = 0;
frm.doc.items.forEach(function(d) {
total += d.amount;
});
frm.set_value('total_amount', total);
},
qty: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, 'amount', (d.qty * d.rate));
var total = 0;
frm.doc.items.forEach(function(d) {
total += d.amount;
});
frm.set_value('total_amount', total);
},
amount: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, 'rate', (d.amount / d.qty));
var total = 0;
frm.doc.items.forEach(function(d) {
total += d.amount;
});
frm.set_value('total_amount', total);
}
});
``````
1 Like

@cpurbaugh
here is my case my child table name is Activityitem and parent doc type is Activity Type
my 3 columns are itemquantity,itemcost and itemtotalost
and what i need is itemquantity * itemcost =itemtotalcost
and here is my code
frappe.ui.form.on(“Activityitem”, {
itemcost: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.Activityitem, ‘itemtotalcost’, (d.itemquantity * d.itemcost));
var total = 0;
frm.doc.items.forEach(function(d) {
total += d.itemtotalcost;
});
frm.set_value(‘total_amount’, total);
},
itemquantity : function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.Activityitem, ‘itemtotalcost’, (d.itemquantity * d.itemcost));
var total = 0;
frm.doc.items.forEach(function(d) {
total += d.itemtotalcost;
});
frm.set_value(‘total_amount’, total);
},
itemtotalcost: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.Activityitem, ‘itemcost’, (d.amount / d.itemquantity));
var total = 0;
frm.doc.items.forEach(function(d) {
total += d.itemtotalcost;
});
frm.set_value(‘total_amount’, total);
}
});
but not working.

@fikerjayz

``````  frappe.ui.form.on("Activityitem", {
itemquantity: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, 'itemtotalcost', (d.itemquantity * d.itemcost));
},
itemcost: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, 'itemtotalcost', (d.itemquantity * d.itemcost));
}
});
``````

Please Check Child Doctype Name, Field Name twice and confirm

but its not making the calculation, what shall i amend
frappe.ui.form.on(“Activityitem”, {
itemquantity: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, ‘itemtotalcost’, (d.itemquantity * d.itemcost));
},
itemcost: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, ‘itemtotalcost’, (d.itemquantity * d.itemcost));
}
});

Post Screen shot of your doctype schema

here is the doctype

@fikerjayz

``````frappe.ui.form.on("Activityitem", {
itemquantity: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, 'itemtotalcost', (d.itemquantity * d.itemcost));
}
});

frappe.ui.form.on("Activityitem", {
itemcost: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, 'itemtotalcost', (d.itemquantity * d.itemcost));
}
});
``````

can you share me your code? I have an identical case which requires custom script like yours… Thanks before…

Study this link. It will help

I’m trying to achieve the same via web form but I’m stuck at the event listener part itself.

This is my code which is working in the form js file:

``````frappe.ui.form.on("Indicator", "value", function(frm, child_doctype, child_name) {
console.log(frm, child_doctype, child_name);
});
``````

and this is code which isn’t working in my webform js file:

``````frappe.web_form.on("Indicator", "value", function(frm, child_doctype, child_name) {
console.log(frm, child_doctype, child_name);
});
``````

Error:
“web_form.js:39 Uncaught TypeError: Cannot read property ‘df’ of undefined”

Any help would be highly appreciated. Thanks!

@saru2020

You can check this Customizing Web Forms

Above will not work for webform.

Thanks for pointing me to the documentation.
In fact, following the same, I’ve got to implement the events for the form fields but the issue is only wrt to accessing the fields of the child table placed inside the webform.

For Example:
For a webform field, this is working:

now, I want to do the same for a field inside a child table which is part of this webform, what should I do for that?

I tried the below options, but isn’t working:

1.)

2.)

Any inputs on how I should approach this problem?

Could you help me with the script. I am trying to change field in child table based on delivery date. I want that if delivery date is within next 5 days, then the field should automatically updated to “This Week”, and if delivery date has been passed then the field should be “Overdue”

``````frappe.ui.form.on('Sales Order Item',"child_table_name_remove",function(frm, cdt, cdn) {
refresh(frm); {
frm.set_value("item_dispatch_sch","Upcoming Week");
frm.refresh_field("delivery_date");
}

else if(frm.doc.delivery_date < frappe.datetime.get_today()) {
frm.set_value("item_dispatch_sch","Overdue");
frm.refresh_field("item_dispatch_sch");
}

frm.refresh_field("delivery_date");}});
``````

pass the child table and name in the `set_value()` method as follows:

`frappe.model.set_value(cdt, cdn, 'fieldNameInQuotes', 'value');`

Then you probably want to refresh the entire table, not the field, i.e. `frm.refresh_field('items')`

Thanks a lot for the immediate reply. I have got it working, but it’s only updated whenever I am making changes in a particular form. But now I want it to keep updating on daily basis for all the submitted documents. Otherwise, it won’t help if the field is only updated while making changes and not automatically on the basis of change in date.
I guess for that we need to write a server script in the schedular event, but am not sure what the script code should be.

Hey… could you help me with the server script as well

@Hardik_Gadesha, @Hafees_Kazhunkil, @cpurbaugh, @laurence could you please kindly help me on this case.
Child table works but it doesnt make sum total above

i dont know why it not work with this script

frappe.ui.form.on(“Chi tiet de nghi”, {
qty:function(frm, cdt, cdn) {
const rowSelected = frm.doc.detail.find(row => row.name === cdn);
rowSelected.amount = rowSelected.qty * rowSelected.rate;
frm.refresh()
},
rate:function(frm, cdt, cdn) {
const rowSelected = frm.doc.detail.find(row => row.name === cdn);
rowSelected.amount = rowSelected.qty * rowSelected.rate;
frm.refresh()
},
})
frappe.ui.form.on(“Chi tiet de nghi”, {
amount:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.detail.forEach(function(d) { total += d.amount; });
frm.set_value(“real_amount”, total);
refresh_field(“real_amount”);
},
detail_remove:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.detail.forEach(function(d) { total += d.amount; });
frm.set_value(“real_amount”, total);
refresh_field(“real_amount”);
},
});