Custom fields calculations

frappe.ui.form.on("Expense Claim Detail", "claim_amount", function(frm) {frm.set_value("claim_amount", frm.doc.qte * frm.doc.prix_unitaire); });

I am using the above script to populate the claim amount field by multiplying the two custom fields Qte and prix_unitaire. Unfortnaletly this is not working. Could anyone help me sort this one out please?

1 Like

@Mute

script are written on claim_amount . So it will work only when you change claim_amount field.
Write it for validate or refresh

e.g.

frappe.ui.form.on("Expense Claim Detail", "validate", function(frm) {
	frm.set_value("claim_amount", (frm.doc.qte * frm.doc.prix_unitaire)); 
});

It will work when you save the form.

1 Like

@Sangram Thank you for replying. Is it possible that the script changes the value of claim amount immediately after inputting the values of qte and prix_unitaire?

@Mute have a try using:

setTimeout(function () {
    frm.set_value("claim_amount", (frm.doc.qte * frm.doc.prix_unitaire));
    refresh_field("claim_amount");
}, 300);

Thank you for replying @JoEz. Unfortunately none of the above solutions are working. The claim_amount field is neither populated automatically or after saving the document. Could anyone help me out please???

on which doctype are the field: qte and prix_unitaire? are that custom fields?

@JoEz Expense Claim Detail is the doctype. qte and prix_unitaire are the custom fields

@Mute

try:

frappe.ui.form.on("Expense Claim Detail", "validate", function(frm, dt, dn) {
        var row = locals[dt][dn];
        frappe.model.set_value(dt, dn, "claim_amount", flt(row.qte * row.prix_unitaire));
        refresh_field("expenses");
}); 

put it as Expense Claim custom script

@JoEz not working. @rmehta could you please look into this please?

where are u putting the script?

i have created a custom script and tried putting it on the Expense claim doctype and the Expense Claim detail doctype @JoEz

On console i see the error Expected declaration but found ‘*’. Skipped to next declaration. What could be the meaning of this?

I finally got it to work using the following script

frappe.ui.form.on(“Expense Claim Detail”, “qty”, function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, “claim_amount”, d.qty * d.prix_unitaire); > });

frappe.ui.form.on(“Expense Claim Detail”, “prix_unitaire”, function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, “claim_amount”, d.qty * d.prix_unitaire);
});`

Just for the sake of future reference, this custom script works only when added to the Parent DocType (at least on V10)

Kind regards,

1 Like