Add Qty and Show in the another field

Hello,

I have created many custom fields under the sales order item or its field type is INT. Now, if I enter data in any of these fields, it will (+) those field ( Qty ) and show in Quantity field. I have attached the screenshot for better understanding.

I referred this post but it has only 2 fields I am getting ( Add ).

Please Help in this.
Thanks.

Hi @falah123,

Please set all fields in the line like:

frappe.model.set_value(cdt, cdn, 'qty', (d.field1 + d.field2 + d.field3 + d.field4));

try it according to the scenario.

Thank You!

If I open a draft sales order and add it, it gets added. If I add it through a new sales order, it does not get added.

I am trying this code, it is getting added but the problem is that when I am entering data for the first time in the field, it is getting removed.

My code :point_down:

frappe.ui.form.on("Sales Order Item", {
    custom_0435: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0536: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0637: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0738: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0839: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0940: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1041: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1142: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1243: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1344: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1445: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1546: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1647: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1748: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
 
    custom_0539: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0640: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0741: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0842: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0943: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1044: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1145: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1246: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1347: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1448: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1549: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1650: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
  
    custom_0319: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0420: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0521: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0622: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0723: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0824: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_0925: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1026: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1127: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1228: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_1329: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
   
    custom_222555: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_3302: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_3403: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_3504: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_3605: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_3706: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_2507: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_2608: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_2709: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_2810: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_2911: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_3012: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    },
    custom_3113: function(frm, cdt, cdn) {
        calculateTotalQty(frm, cdt, cdn);
    }

});

function calculateTotalQty(frm, cdt, cdn) {
    var d = locals[cdt][cdn];
    var totalQty =(d.custom_0435 || 0) + (d.custom_0536 || 0) + (d.custom_0637 || 0) + (d.custom_0738 || 0) + (d.custom_0839 || 0) + (d.custom_0940 || 0) + (d.custom_1041 || 0) + (d.custom_1142 || 0) + (d.custom_1243 || 0) + 
                  (d.custom_1344 || 0) + (d.custom_1445 || 0) + (d.custom_1546 || 0) + (d.custom_1647 || 0) + (d.custom_1748 || 0) + (d.custom_0539 || 0) + (d.custom_0640 || 0) + (d.custom_0741 || 0) + (d.custom_0842 || 0) +  
                  (d.custom_0943 || 0) + (d.custom_1044 || 0) + (d.custom_1145 || 0) + (d.custom_1246 || 0) + (d.custom_1347 || 0) + (d.custom_1448 || 0) + (d.custom_1549 || 0) + (d.custom_1650 || 0) + (d.custom_0319 || 0) +
                  (d.custom_0420 || 0) + (d.custom_0521 || 0) + (d.custom_0622 || 0) + (d.custom_0723 || 0) + (d.custom_0824 || 0) + (d.custom_0925 || 0) + (d.custom_1026 || 0) + (d.custom_1127 || 0) + (d.custom_1228 || 0) +
                  (d.custom_1329 || 0) + (d.custom_3302 || 0) + (d.custom_3403 || 0) + (d.custom_3504 || 0) + (d.custom_3605 || 0) + (d.custom_1650 || 0) + (d.custom_3706 || 0) + (d.custom_2507 || 0) + (d.custom_2608 || 0) + 
                  (d.custom_2709 || 0) + (d.custom_2810 || 0) + (d.custom_2911 || 0) + (d.custom_3012 || 0) + (d.custom_3113 || 0) + (d.custom_222555 || 0);
    frappe.model.set_value(cdt, cdn, 'qty', totalQty);
}

Thanks.

Hi @falah123

I think your code is correct. Just change it. I hope it will work

frappe.model.set_value(cdt, cdn, 'qty', d.field_1 + d.field_2 + d.field_3):

Thank You!

Did not Work.

Is there any solution to this problem?
If anyone knows, please help.
Thank you.

The problem with this code is that whenever we input value, save the previous value. Lets understand with example, Data is present in all fields ( field1= 1 ), ( field2= 0 ) , ( field3= 4 ), When we input value in the field, the previous value in it is returned. If the ( field1= 1 ) or I did ( 1 to 10 ) so it automatically takes ( 1 ). If the ( field2= 0 ) or I did ( 0 to 5) so it automatically takes 0.

I have attached a video below for better understanding. :point_down:

Test-SAL-ORD-2024-00001-GoogleChrome2024-04-0912-55-30-ezgif.com-video-to-gif-converter

Is there any solution to this problem?
If anyone knows, please help.
Thank you.

Seems like timing issue? Try adding frm.refresh_field or set a timeout.

Also create an array for your fields for easier code readability.

I am not that good at coding, I have just started learning coding. Can you write and give it to me please?