One single doctype : Parent
Child table : Child
In Parent there are two fields
Net salary
Total Salary
In Child Table only one field
Incentives
I want to add Net salary and Incentives and store the sum in Total salary
Can anybody please help me how to do this ?
@bnmahesh0017 Assuming the following:
The fieldname of Net salary
field is net_salary
The fieldname of Total Salary
field is total_salary
The fieldname of the child table Child
is incentives_list
The fieldname of the Incentives
field is incentives
What you need to do is create a new Client Script the set the doctype to Parent
, the apply on to Form
and paste the following code.
function update_total_salary(frm) {
let doc = frm.doc,
total = 0;
if (doc.net_salary) total += flt(doc.net_salary);
if (doc.incentives_list.length) {
$.each(doc.incentives_list, function(i, r) {
total += flt(r.incentives);
});
}
frm.set_value('total_salary', total);
}
frappe.ui.form.on('Parent', {
net_salary: function(frm) {
update_total_salary(frm);
}
});
frappe.ui.form.on('Child', {
incentives_list_add: function(frm, cdt, cdn) {
update_total_salary(frm);
},
incentives_list_remove: function(frm, cdt, cdn) {
update_total_salary(frm);
}
});
@kid1194
Its not working as I expected.
Instead it returns the same value to total_salary which we give in net_salary . so I don’t want that.
what ever the parameters you have assumed is correct.
For me how it should work is :-
net_salary + the highest among the list of incentives = total_salary
Please help me out with this ?
@bnmahesh0017 I didn’t know about the highest incentive. The following code works as you want.
The total_salary
will be calculated when:
The value of net_salary
changed
A row is added or removed from the child table
function update_total_salary(frm) {
let doc = frm.doc,
incentive = 0,
total = flt(doc.net_salary);
if (doc.incentives_list.length) {
$.each(doc.incentives_list, function(i, r) {
let ival = flt(r.incentives);
if (ival > incentive) incentive = ival;
});
total += incentive;
}
frm.set_value('total_salary', total);
}
frappe.ui.form.on('Parent', {
net_salary: function(frm) {
update_total_salary(frm);
}
});
frappe.ui.form.on('Child', {
incentives_list_add: function(frm, cdt, cdn) {
update_total_salary(frm);
},
incentives_list_remove: function(frm, cdt, cdn) {
update_total_salary(frm);
}
});
Sorry to say even this code doesn’t worked for me.
Parent doctype
Child Doctype
Entries to the Parent doctype
Now I want to add Net Salary and in Incentives list the highest incentive i.e
2000 and store the value in Total Salary Please help me out with this.
kid1194
September 1, 2022, 5:11pm
6
@bnmahesh0017 It is not supposed to work because some fieldnames are wrong…
function update_total_salary(frm) {
let doc = frm.doc,
incentive = 0,
total = flt(doc.net_salary);
if (doc.incentives_list.length) {
$.each(doc.incentives_list, function(i, r) {
let ival = flt(r.incentive);
if (ival > incentive) incentive = ival;
});
total += incentive;
}
frm.set_value('total_salary', total);
}
frappe.ui.form.on('Parent', {
net_salary: function(frm) {
update_total_salary(frm);
}
});
frappe.ui.form.on('Child', {
incentives_list_add: function(frm, cdt, cdn) {
update_total_salary(frm);
},
incentives_list_remove: function(frm, cdt, cdn) {
update_total_salary(frm);
}
});
kid1194
September 1, 2022, 5:54pm
7
@bnmahesh0017 The code will update the total_salary
field when you add or change net_salary
field and when you add or remove an incentive…
So at first without any incentive, the value will be equal to the net_salay
field. But as soon as you add an incentive it will change…