Math Functions Child Table

Hello,
I have 2 filds in a Child table ( “cantitate” and “pret”) and the field " total" in custom doctype , Necesar Materiale"

What I am trying to do is to have the following formula cantitate x pret= total. But the total needs to contain the multiply of all the rows added.

I have the following script but is not working

frappe.ui.form.on(‘Necesar Materiale’, {
onload: function(frm) {
//This will make sure they are are updated whenever the form reloads
frm.set_value(“total”, cur_frm.doc.cantitate * cur_frm.doc.pret_2);
},
cantitate: function(frm) {
//This will make sure the calculation happens whenever cantitate is updated
frm.set_value(“total”, cur_frm.doc.cantitate * cur_frm.doc.pret_2);
},
pret_2: function(frm) {
//This will make sure the calculation happens whenever pret is updated
frm.set_value(“total”, cur_frm.doc.cantitate * cur_frm.doc.pret_2);
},
});

Try with this -

frappe.ui.form.on(‘Child Table Name’, {
cantitate: function(frm, cdt, cdn){
    // Calculate when change Cantitate value
    var d = locals[cdt][cdn];
    if(d.pret_2){
         cur_frm.set_value(“total”, d.cantitate * d.pret_2);
         refresh_field("total");
    }
},
pret_2:  function(frm, cdt, cdn){
    // Calculate when change Pert value
    var d = locals[cdt][cdn];
    if(d.cantitate){
        cur_frm.set_value(“total”, d.cantitate * d.pret_2);
        refresh_field("total");
    }
}
});

@priya_s

I receive this erorr

SyntaxError: Invalid or unexpected token
at Class.setup (http://erp.grupelectroinstal.com:98/assets/js/form.min.js?ver=1513023181.0:2628:18)
at _f.Frm.setup (http://erp.grupelectroinstal.com:98/assets/js/form.min.js?ver=1513023181.0:172:22)
at _f.Frm.refresh (http://erp.grupelectroinstal.com:98/assets/js/form.min.js?ver=1513023181.0:441:9)
at Class.load (http://erp.grupelectroinstal.com:98/assets/js/form.min.js?ver=1513023181.0:87:33)
at http://erp.grupelectroinstal.com:98/assets/js/form.min.js?ver=1513023181.0:82:7
at Object.callback (http://erp.grupelectroinstal.com:98/assets/js/desk.min.js?ver=1513023181.0:5553:6)
at Object.callback [as success_callback] (http://erp.grupelectroinstal.com:98/assets/js/desk.min.js?ver=1513023181.0:1428:16)
at _ (http://erp.grupelectroinstal.com:98/assets/js/desk.min.js?ver=1513023181.0:1452:34)
at Object. (http://erp.grupelectroinstal.com:98/assets/js/desk.min.js?ver=1513023181.0:1553:5)
at i (http://erp.grupelectroinstal.com:98/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

and the value of ,total" is not updated.

Can you please share your code ?

frappe.ui.form.on(‘Necesar Materiale’, {
onload: function(frm) {
//This will make sure they are are updated whenever the form reloads
frm.set_value(“total”, cur_frm.doc.cantitate * cur_frm.doc.pret_2);
},
cantitate: function(frm) {
//This will make sure the calculation happens whenever cantitate is updated
frm.set_value(“total”, cur_frm.doc.cantitate * cur_frm.doc.pret_2);
},
pret_2: function(frm) {
//This will make sure the calculation happens whenever pret is updated
frm.set_value(“total”, cur_frm.doc.cantitate * cur_frm.doc.pret_2);
},

@priya_s

@Florea_Andrei try this:

frappe.ui.form.on(‘Necesar Materiale’, {
onload: function(frm) {
//This will make sure they are are updated whenever the form reloads
frm.set_value(“total”, frm.doc.cantitate * frm.doc.pret_2);
frm.refresh_field("total");
},
cantitate: function(frm) {
//This will make sure the calculation happens whenever cantitate is updated
frm.set_value(“total”, frm.doc.cantitate * frm.doc.pret_2);
frm.refresh_field("total");
},
pret_2: function(frm) {
//This will make sure the calculation happens whenever pret is updated
frm.set_value(“total”, frm.doc.cantitate * frm.doc.pret_2);
frm.refresh_field("total");
}
});

@Florea_Andrei,

I think you wrote a trigger on your main form. So you need to write it on you Child Table trigger instead of main form.
Please refer my previous reply…

Hi @priya_s still not working:(

Total remains 0.

Hi
Between the two functions u need a semi-colon (:wink: to seperate.

Regards