How to divide two fields value and store the output in the 3 field

I have 3 fields A,B and C
and I want C = A/B in ERP Next
For that, I have written the below code in the client script of sales invoice item,

frappe.ui.form.on(“Sales Invoice Item”,{
refresh : function(frm) {
frm.set_value(“total_cartons”, (frm.doc.stock_qty / frm.doc.units_in_carton));
}
});

But it is not working please help me…
Thanks A Lot

@Stellar can you share the console log when Sales Invoice Item is refreshed

Hi @Stellar,

Please check it.

Thank You!

Hi @DineshLiyanage sorry I don’t have access to the console log

Hi @Solufy I already tried this but it is not fetching the values for me not be able to understand what is the problem as I it is not showing any error as well.

Please check it.

frappe.ui.form.on("Sales Invoice Item", {
    units_in_carton: function(frm,cdt,cdn) {
        var d = locals[cdt][cdn];
        frappe.model.set_value(cdt, cdn, 'total_cartons', (d.stock_qty / d.units_in_carton));
    }
});

Then reload and check it.

Thank You!

Hi @Solufy where did I write this client script,
In the client script of the “Sales Invoice” or in the Client script of the “Sales Invoice Item”

Hi @Stella,

Check it.

Hi @Solufy It is still not fetching the value in the Total Cartons Field below I’m providing the screenshot as well.

I’m also sending you the screenshot of the client script where I have added the code.
Thanks A Lot


units_in_carton added manually or automatic?

If manually added then blow script work 100%.

Value in Units_in_carton is fetching from the Item List. I have declared the value for this there.
Can’t we do it like that ??

Hi @Stellar,

Please apply and check it.

When you save it then value automatically set in field.

frappe.ui.form.on('Sales Invoice',  {
    before_save: function(frm) {
        $.each(frm.doc.items,  function(i,  d) {
            d.total_cartons = d.stock_qty / d.units_in_carton;
            frm.refresh_field('items');
        });
    } 
});

Hi @Solufy I’m extremely sorry for giving you so much trouble but it is still not working.
No value is being fetched to that “total_cartons” field.

Let me explain you all the scenario again:-
There are two field in the Sales Invoice Item table
stock_qty and units_in_carton
and the value in stock_qty is added by the user
and I have defined the value of units_in_carton in the ITEM LIST or Item Master what ever you want to say it like.

Now,
I have opened another field with name “total_cartons”
Now I just want that the value of “total_cartons” would be stock_qty/ units_in_carton.

Please guide me and Thank You So Much for the help

Same scenario added in my system.
It woks fine with this script.

Hi @Solufy don’t know what’s the issue with it I still can’t be able to understand it.
I have written down the code correctly, still the field Total_cartons is coming blank.

Hi @Stellar,

again try it.

frappe.ui.form.on("Sales Invoice Item", {
    qty: function(frm,cdt,cdn) {
        var d = locals[cdt][cdn];
        frappe.model.set_value(cdt, cdn, 'total_cartons', (d.stock_qty / d.units_in_carton));
    }
});

Thank You!

Hi @Solufy it’s working now, I need a very small changement in this .
Like if the stock qty = 36 and the units in carton = 12 then then output would be total cartons = 3
And for the case where stock qty = 40 but the units in carton is still = 12 so I want it to fetch the value in total carton still 3.
Like only the closest integer value not a float value in the total cartons field.

@Solufy Please Help…