Parent field trigger to child table populate

Hi, i’am trying to make a custom script
if i click the ‘Mode of Payment = OVO’, can i add row on child table ‘Payment Entry Deduction’ automatically

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

can you help me?

image
image

Hi @ceem123,

How are you?
After two days…

please apply script:

frappe.ui.form.on('Payment Entry',  {
    mode_of_payment: function(frm) {
        if (frm.doc.mode_of_payment == "OVO")
        {
       var row = frappe.model.add_child(cur_frm.doc, "Payment Entry Deduction", "deductions");
        row.account = "45.02.04 - Beban Fee OVO - HO";
        row.amount = 0.2 * frm.doc.paid_amount;
        frm.refresh_fields("deductions");
        }
    }
}); 

Thank You!

Hi, i’m fine, and you?
Btw, thank you for your reply

i just try it, but the amount is error = NaN
maybe because, i put paid amount after choose the mode of payment?

I try fill the paid amount first and then i choose the mode of payment
the amount is correct

how to make that’s not error if i choose the mode of payment first?

Thank you

I am Fine!

So check trigger value
mode_of_payment: function(frm) { → paid_amound: function(frm) {

Or otherwise
you can select paid amount first then select mode of payment.

Thank You

It’s work!
But, if i change the mode of payment, the row in child table still there
can i delete it if i change the mode of payment?

Hi @ceem123,

So please add another script:

frappe.ui.form.on('Payment Entry',  {
    mode_of_payment: function(frm) {
        if (frm.doc.mode_of_payment != "OVO")
        {
       frm.doc.deductions = [];
       frm.refresh_fields("deductions");
        }
    }
}); 

It’s work … :slightly_smiling_face:
Thank You!

It’s work, but i have other problems hehe

i add logic for other mode of payment
but if first i choose ovo, then i change to gopay, the child table it’s gone
but the function of gopay didn’t work

maybe we could have refresh function on mode of payment field?
Thank You!

So you can try merge script then check.
If not work then i will try it.
Ok.

Have a good day.
and hope you can do it with done.

Thank You!

Hi @ceem123,

If your trial script does not work, then use this script.
It works with testing.

And check your variable and account name in scripting.

frappe.ui.form.on('Payment Entry',  {
    mode_of_payment: function(frm) {
        cur_frm.clear_table("deductions");
        if (frm.doc.mode_of_payment == "OVO")
        {
            var row = frappe.model.add_child(cur_frm.doc, "Payment Entry Deduction", "deductions");
            row.account = "45.02.04 - Beban Fee OVO - HO";
            row.cost_center = "Main - HO";
            row.amount = 0.2 * frm.doc.paid_amount;
        }
        
        if (frm.doc.mode_of_payment == "GoPay")
        {
            var row1 = frappe.model.add_child(cur_frm.doc, "Payment Entry Deduction", "deductions");
            row1.account = "45.02.01 - Beban Fee GoPay - HO";
            row1.cost_center = "Main - HO";
            row1.amount = 0.2 * frm.doc.paid_amount;
        }
        
        if (frm.doc.mode_of_payment != "OVO" && frm.doc.mode_of_payment != "GoPay")
        {
            frm.doc.deductions = [];
        }
        
        frm.refresh_fields("deductions");
    }
}); 

Thank You!

Hi,thank you!
It’s work if i first fill the paid amount field
but my flow is fill the mode of payment first (remember the amount is NaN?)
the previous coding we use paid_amount: function on top

Thank you!

Hi @ceem123,

Because you do not enter paid amount then how to set amount in child table.
and same thing why not use paid_amount trigger. If you select paid_amount then automatically process work same as a mode of payment.

otherwise you can use before_save trigger.

Thank You!

Hi @NCP
if i use paid_amount trigger, the function of

if (frm.doc.mode_of_payment != “OVO” && frm.doc.mode_of_payment != “GoPay”)
{
frm.doc.deductions = [];
}

didn’t work

Can i add the function if paid_amount is blank, the mode_of_payment function didn’t work?
Thank you!