Calculate Total from Several Sub Total

Hello all,

I made an audit form which consist of several sub total number. There is 5 sub total item and I want to calculate all 5 sub total into a TOTAL. How can I make this happens? I tried googling and made this code below but it was not functioned:

frappe.ui.form.on(“Audit 5S”, “sub_total”, function(frm, cdt, cdn) {
var sub_total = frm.doc.audit_5s;
var total = 0
for(var i in sub_total) {
total = total + sub_total_1 + sub_total_2 + sub_total_3 + sub_total_4 + sub_total_5
}

frm.set_value("total",total)

});

Below is the screenshot of the doctype:

I am completely noobs in coding…

Kindly your help…

1 Like

Hi I think you should use Child table instead see Calculation in Child Table. But anyway, you can continue what you have started. I’m wondering why you have a for loop in your block

for(var i in sub_total) {
total = total + sub_total_1 + sub_total_2 + sub_total_3 + sub_total_4 + sub_total_5
}

I think it should be

total = frm.doc.sub_total_1 + frm.doc.sub_total_2 + frm.doc.sub_total_3 + frm.doc.sub_total_4 + frm.doc.sub_total_5

Hi!
Assuming that sub_total_1 to sub_total_5 has values, I think the following code should work. :slight_smile:
Btw, “validate” on the frappe.ui.form.on is the form trigger that occurs when the user saves the document. So this code snippet executes once you save the document.

frappe.ui.form.on("Audit 5S", "validate", function (frm){
	var subtotal1 = frm.doc.sub_total_1;
	var subtotal2 = frm.doc.sub_total_2;
	var subtotal3 = frm.doc.sub_total_3;
	var subtotal4 = frm.doc.sub_total_4;
	var subtotal5 = frm.doc.sub_total_5;

	var total = subtotal1+subtotal2+subtotal3+subtotal4+subtotal5;
	frappe.model.set_value(frm.doctype, frm.docname, "total", total);
	frm.refresh();

});
1 Like

Hello,

Tried this one but it’s not working…

Thanks

Thank you.

Salam,
D

Hello @cyberdee

Can you post your code so that we can also check the syntax?

By the way, just wondering on if the subtotals are stored in a Child Table Document or if they’re in the Main/Parent doctype. It would help if you could specify if which document is the parent document and which one is the child table document.

Thanks.

@littlerhera

here you goes…

frappe.ui.form.on(“Audit 5S”, “validate”, function (frm){
var subtotal1 = frm.doc.sub_total_1;
var subtotal2 = frm.doc.sub_total_2;
var subtotal3 = frm.doc.sub_total_3;
var subtotal4 = frm.doc.sub_total_4;
var subtotal5 = frm.doc.sub_total_5;