How to auto calculate on child table

Hi guys,
I am trying too automatically calculate field on child table

what i want is
if account field = ‘45.02.04 - Beban Fee Ovo - HO’ , cost center field = ‘Main - HO’, and amount field = 0.2 * paid amount field (above : amount section)

i’m trying use this logic, but it doesn’t work
//frappe.ui.form.on(‘Payment Entry Deduction’,‘account’,function(frm, cdt, cdn) {
//let item=locals[cdt][cdn]
//let amount=0
//if(!frm.doc.account) {
//amount = 2;
//} else if(frm.doc.account) {
// amount = 3;
// }
//item.amount=amount
//frm.refresh_field(‘deductions’)
//});
can you help me, please?

Hi @ceem123,

Please apply client script:

frappe.ui.form.on('Payment Entry Deduction','account',function(frm, cdt, cdn)
    {
       let d=locals[cdt][cdn];
       if(d.account == "45.02.04 - Beban Fee Ovo - HO")
           {
            d.amount = 0.2 * frm.doc.paid_amount;
          //d.amount = 0.2 * flt(frm.doc.paid_amount); (if not work above line then use)
            }
        else{
                d.amount = 3;
            }
            frm.refresh_field('deductions');
});

It’s really work for you.

Thank You!

Hi, thank you for your reply

but that’s didn’t work for me
in field account i use 45.02.04 - Beban Fee Ovo - HO, but the amount is 3
what ever i choose the account, the amount just 3

image

Hi @ceem123,

Please check you variable field set proper in script or not.

After that defined static value like:

frappe.ui.form.on('Payment Entry Deduction','account',function(frm, cdt, cdn)
    {
       let d=locals[cdt][cdn];
       if(d.account == "45.02.04 - Beban Fee Ovo - HO")
           {
          // static value print 
            d.amount = 3
            }
        else{
          // static value print 
               d.amount = 2;
            }
            frm.refresh_field('deductions');
});

Do not try anything only first try to set static value then check.
Only try for account after that try for cost center.

Thank You!

And also remove unnecessary script.
Thank You!

i think the ‘45.02.04 - Beban Fee Ovo - HO’ it’s wrong
but i don’t have any idea about it

the result just same, it’s all amount = 2


Hi @ceem123,

Check you account name:
Your Account Name is “45.02.04 - Beban Fee OVO - HO”
so set account name in script.

Different is: Ovo → OVO

Thank You!

OMG, i’m not notice that capital letter
It’s worked, thanks a lot!

Let me ask a question again
if i click the ‘Mode of Payment = OVO’, can i add row on child table ‘Payment Entry Deduction’ automatically with previous coding?

‘Mode of Payment’ is custom doctype

What i want is
When i choose OVO in Mode of Payment
Automatically add row in child table Payment Entry Deduction, which is the formula is account = Beban Fee OVO, amount = 0.2*paid amount (previous coding)


Thank you so much!

1 Like

Hi @ceem123,

It’s a very difficult task because parent field trigger to child table populate.

some suggestion like:

Hope are you understand some code.

Thank You!