Been trying to have a mathematical formula in a custom script.
I have 3 fields:
Length= len
Width= wid
Area= area
I’ve tried the following formula, that I found in an older post and changed the values, but no luck:
// add a trigger on field "len"
cur_frm.cscript.len = function(doc, cdt, cdn) {
// update a new field "area"
doc.area = flt(doc.len)*flt(doc.wid);
// refresh in form
refresh_field('area');
}
// add the same trigger on "wid"
cur_frm.cscript.wid = cur_frm.cscript.len;
My main doctype is “Quotemaster”
Child Doctype is “Quotemaster Area”
Fields in “Quotemaster Area”
len - Length - Float (originally int)
wid - Width - Float
hei - Height - Float (not used for this)
area - Area - Read Only (I have tried using types: int, float, read only)
when do you are in a child form a small change is required, try this
frappe.ui.form.on("Quotemaster Area", "len", function(frm, cdt, cdn){
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, "area", d.len * d.wid);
});
Keep in mind that, the value of area will be updated based in others fields, so you need add the same trigger for all of the these fields, and never in the updated field itself
It’s not clear to me what you said in the last line there, but in the “Quotemaster” doctype, the “Quotemaster Area” table name is “area”. So, I put that. No change in result…
frappe.ui.form.on("Quotemaster Area", "len", function(frm, cdt, cdn){
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, "area", d.len * d.wid);
frm.fields_dict["area"].grid.grid_rows_by_docname[cdn].area.refresh();
});
frappe.ui.form.on("Quotemaster Area", "wid", function(frm, cdt, cdn){
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, "area", d.len * d.wid);
frm.fields_dict["area"].grid.grid_rows_by_docname[cdn].area.refresh();
});
@cpurbaugh this code looks nice! But if you have modifications in frappe code or ERPNext code, will be great if you check, because the messages in your console are causing your issue.