diff --git a/Community-Developed-Custom-Scripts.md b/Community-Developed-Custom-Scripts.md index c6fa886..8d2e065 100644 --- a/Community-Developed-Custom-Scripts.md +++ b/Community-Developed-Custom-Scripts.md @@ -85,6 +85,29 @@ frappe.ui.form.on("Dropbox Backup", "refresh", function(frm){ ``` *** +### Calculating Values in Child Tables + +to calculate the values of child tables, you can use the following functions: +``` +frappe.ui.form.on("[CHILD TABLE DOCTYPE]", { + [CHILDTABLEMULTIPLE1]: function(frm, cdt, cdn) { + var d = locals[cdt][cdn]; + var total = 0; + frappe.model.set_value(d.doctype, d.name, "[CHILDTABLEPRODUCT]", d.[CHILDTABLEMULTIPLE1] * d.[CHILDTABLEMULTIPLE2]); + frm.doc.[CHILDTABLEFIELDINPARENT].forEach(function(d) { total += d.[CHILDTABLEPRODUCT]; }); + frm.set_value('[PARENTDOCTYPETOTALFIELD]', total); + } +}); +frappe.ui.form.on("[CHILD TABLE DOCTYPE]", { + [CHILDTABLEMULTIPLE2]: function(frm, cdt, cdn) { + var d = locals[cdt][cdn]; + var total = 0; + frappe.model.set_value(d.doctype, d.name, "[CHILDTABLEPRODUCT]", d.[CHILDTABLEMULTIPLE1] * d.[CHILDTABLEMULTIPLE2]); + frm.doc.[CHILDTABLEFIELDINPARENT].forEach(function(d) { total += d.[CHILDTABLEPRODUCT]; }); + frm.set_value('[PARENTDOCTYPETOTALFIELD]', total); + } +}); +``` ### Fetch child table The following fetches all entries in a child table from another document.