Change child variable AFTER erpnext makes his calculations

Hi there people, Im trying to multiply 2 variables, “qty” and m"argin rate or amount", both are in a child table, i already did it, but i have a problem…
When I choose Percentage and i Put some value for example… “5”, erpnext changes the value of “margin_rate_or_amount” which is perfect, but my variable stays in “5”. and my result goes wrong, my code multiplies 5qty, and i want to multiple te result of the convertqty.
So, I want to execute my function AFTER my variables changes his value.

frappe.ui.form.on('Sales Order Item', {
    margin_rate_or_amount:function(frm, cdt, cdn){
    var c = locals[cdt][cdn];
    frappe.model.set_value(cdt, cdn, "rentabilidad_real", c.margin_rate_or_amount*c.qty);
    frm.refresh();
 },
    qty:function(frm, cdt, cdn){
    var c = locals[cdt][cdn];
    frappe.model.set_value(cdt, cdn, "rentabilidad_real", c.margin_rate_or_amount*c.qty);
    frm.refresh();
    }
});

THANKS!

Hello @Axel_Gavilan,

Welcome to the Forum!

I’ve read over your requirements a few times. I am confused. Are you trying to execute some calculation after “rentabilidad_real” gets changed? If this is the case just add another method like you did with “qty” and “margin_rate_or_amount”.

Hi dj12djdjs! thanks for response!

I will try to explain better.
child table ITEM, has a field “MARGIN TYPE”, it has 2 options, AMOUNT or PERCENTAGE, if I choose amount, my code works well, becouse i have to write the amount of margin and then multiply with QTY, and the results is OK, but if I choose PERCENTAGE, i put the percentage amount for example " 5%" and my code multiplies 5*QTY. and te result is wrong.
I want my code Execute AFTER erpnext already did the conversion from % to amount in USD.
Sorry, Its complicated to understand becouse Im using native field “margin rate or amount”. I hope you got a solution. Thank you

After using Google translate to figure out what rentabilidad_real means in English, I believe you are trying to calculate the profitability of that item.

If this is what you’re trying to accomplish, this functionality is in ERPNext by default. There is a field called Gross Profit in sales order item. It will show your profitably of that item.

Is this what you’re looking for?