Sum a table's row

Hey,

I have created a child table in Employee DocType because our HR users doesn’t want to use Leave Modules. Instead we have a created table in Employee module and they want to add a row every time that employee earned or used a leave.

For example;

Annual Leave - 01.01.2022 - 01.01.2023 - 20 - Earned
Annual Leave - 31.05.2022 - 01.06.2022 - 1 - Used
Annual Leave - 31.06.2022 - 02.07.2022 - 2 - Used

Total Leaves Left: 18

I don’t have much of Javascript knowledge so if you explain to me like I’m 5, it would be great.

Hi @Yildirim_Hakan_Savci,

Please apply it.

frappe.ui.form.on('DocType Name', {
    validate: function(frm,cdt,cdn) {
        set_total_leave(frm);
    }
});

function set_total_leave(frm) {
    var doc = locals[frm.doc.doctype][frm.doc.name];
    var total_leave = 0;
    //Example for table name => $.each(doc.items, function(i, d) {
    $.each(doc.Your_Table_Name, function(i, d) {
        total_leave += flt(d.annual_leave);
    });

frm.set_value("total_leave",total_leave);
}

Set your doctype name, table, and table field name according to.

Thank You!

Hello there,

I was on a leave. Now i saw your message.

I will try and come back to you.

Thank you so much.

Hi there,

My table looks like this.

Doctype Name is Employee but this is a child table and name is “Leave Table”

I want to calculate “Toplam Gün” Table Field and write it to “Toplam İzin” field.

So on your script;

I couldn’t fill the blanks you have left for me :slight_smile: Would you help me out more on that? Thank you

frappe.ui.form.on('Employee', {
    validate: function(frm,cdt,cdn) {
        set_total_leave(frm);
    }
});

function set_total_leave(frm) {
    var doc = locals[frm.doc.doctype][frm.doc.name];
    var total_leave = 0;
    $.each(doc.leave_table, function(i, d) {
        total_leave += flt(d.annual_leave);
    });

frm.set_value("total_leave",total_leave);
}

Please check your field name in customize form of Employee and set your according.
It’s working on my side version 13.

Thanks You!

Thanks for your patience :slight_smile:

But… I haven’t quite make it work yet. I want those integers on my table to sum up in the field below.

Here is my field names.

frappe.ui.form.on('Employee', {
    validate: function(frm,cdt,cdn) {
        set_total_leave(frm);
    }
});

function set_total_leave(frm) {
    var doc = locals[frm.doc.doctype][frm.doc.name];
    var total_leave = 0;
    $.each(doc.tablo, function(i, d) {
        total_leave += flt(d.toplam_gum);
    });

frm.set_value("toplam_izin",total_leave);
}

I don’t understand your language.

So check your field and set it in the script according to.
Also, check the child table field and set it in the script.

Thanks.

And if the below script does not work then apply another script.

frappe.ui.form.on('Employee',  {
    validate: function(frm) {
        total_leave = 0;
        $.each(frm.doc.tablo,  function(i,  d) {
            total_leave += flt(d.toplam_gum);
        });
        frm.doc.toplam_izin = total_leave;
    } 
});

Thanks.