Mathematical Operations

Hey guys,

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;

@cpurbaugh try use the new style for javascript custom handlers

frappe.ui.form.on("DocType", "fieldname", function(frm){
    frappe.model.set_value(frm.doctype, frm.docname, 'area', frm.doc.len*frm.doc.wid);
});
2 Likes

Still not working. Should I have anything in the ‘options’ in the area field? Also, this is a child table, does that affect anything?

@cpurbaugh is the same for a Child table, but in “DocType” it’s the name of the child doctype!

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)

Custom Script:

frappe.ui.form.on("Quotemaster Area", "area", function(frm){
    frappe.model.set_value(frm.doctype, frm.docname, 'area', frm.doc.len*frm.doc.wid);
});

@cpurbaugh my fail!

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

  1. List item
1 Like

@max_morais_dmm Still not working, and I’m not sure where I’m going wrong here.

Show me your code, it will be util to help you!

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);
});

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);
});

@cpurbaugh do you have any message in browser console? Press F12 to check this!

The only thing that comes up is this:

unreachable code after return statement

Maybe the value already are in the field, but it’s not refreshing let’s try tp force the field refresh on screen.

add this stretch of code, below frappe.model.set_value

frm.fields_dict["grid-field-name"].grid.grid_rows_by_docname[cdn].area.refresh();

replace the grid-field-name by the name of the Table Field of Quotemaster Area

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 can you give me print-screen of your console?

@cpurbaugh :construction_worker: have something very strange in your code!

I’ll be greath if you share the full custom scripts for we undestand what is going wrong!

Unreachable code after return statement

Never sounds like a good thing!

That is the full script…Also, it says unreachable code after return statement on all pages, even at the desk.

@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.

I don’t have any customizations in the source code at all, and this is my only custom script…

@cpurbaugh So, try update ERPNext and frappe, and check if the message still!

Some cases, certain extensions in the browser can cause strange errors too.

Will try update tomorrow. Also, Microsoft Edge gives this error: