Client script to add 3 field values in a single field

Dear Awesome Team,

I wish to add 3 field (Basic & DA & Gen Allowance) values in field CTC.
I have written client script for that but it applies when I do the changes in the Basic field ONLY, when I do the changes in the other two fields DA and Gen Allowance, changes are not reflected in the CTC field.

Client SCript is below:
frappe.ui.form.on(“Employee”, {
custom_basic: function(frm,cdt,cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, ‘ctc’, (d.custom_basic + d.custom_da + d.custom_general_allowance));
}
});

Pls find the Form screenshot:

Pls add the code for the other two fields as well.

Thanks in advance. all the support really appreciated.

@Sohailans
you need to make function for other 2 custom_da and custom_general_allowance

@Sohailans

custom_da : function(frm,cdt,cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, ‘ctc’, (d.custom_basic + d.custom_da + d.custom_general_allowance));
},

custom_general_allowance: function(frm,cdt,cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, ‘ctc’, (d.custom_basic + d.custom_da + d.custom_general_allowance));
},

Thanks Jenish, really appreciate your support.

You can’t use frappe.model.set_value to set a value in the parent doctype; it is used for the child table doctype.

If you want to set a value in the parent doctype, use the frm.set_value.

frappe.ui.form.on('Employee', {
    basic: function(frm) {
        calculate_ctc(frm);
    },
    da: function(frm) {
        calculate_ctc(frm);
    },
    general_allowance: function(frm) {
        calculate_ctc(frm);
    }
});

function calculate_ctc(frm) {
    let ctc = (frm.doc.basic || 0) + (frm.doc.da || 0) + (frm.doc.general_allowance || 0);
    frm.set_value('ctc', ctc);
}

Please set the doctype name and field name in the script.

1 Like

really Thanks NCP