Calculate a value for custom field

@kolate_sambhaji @rmehta, Hi guys, I have items that are sold by piece units, but we need to input weight * price per weight = total price per weight, just to print in the sales order with no extra function in the system, so I have made 3 custom fields:

  1. item_weight (manual input)
  2. item_weight_price (manual input)
  3. item_weight_price_total (auto input “item_weight * item_weight_price = item_weight_price_total”)

I use this script that I took from above:

frappe.ui.form.on(“Sales Order Item”, “item_weight_price_total”, function(frm, doctype, name) {
var row = locals[doctype][name];
row.item_weight_price_total = row.item_weight * row.item_weight_price;
refresh_field(“item_weight_price_total”);
});

Hope you can help me out, thank you so much!

Mo

write script on item_weight and item_weight_price,
So on changing item_weight/item_weight_price, item_weight_price_total will be calculated

   frappe.ui.form.on("Sales Order Item", "item_weight", function(frm, doctype, name) {
          var row = locals[doctype][name];
          row.item_weight_price_total = row.item_weight * row.item_weight_price;
          refresh_field("item_weight_price_total");
        });
    frappe.ui.form.on("Sales Order Item", "item_weight_price", function(frm, doctype, name) {
      var row = locals[doctype][name];
      row.item_weight_price_total = row.item_weight * row.item_weight_price;
      refresh_field("item_weight_price_total");
    });
1 Like

@kolate_sambhaji thank you for your help.
Your code works only if I change

refresh_field(“item_weight_price_total”);

into the following:

refresh_field(“items”);

But then I have to enter something in field “item_weight_price_total” manually to refresh. I appreciate your help, thanks.

Hi @kolate_sambhaji I have got it set with the following script

frappe.ui.form.on(“Sales Order Item”, “item_weight”, function(frm, doctype, name) {
var row = locals[doctype][name];
row.item_weight_price_total = row.item_weight * row.item_weight_price;
refresh_field(“items”);
});
frappe.ui.form.on(“Sales Order Item”, “item_weight_price”, function(frm, doctype, name) {
var row = locals[doctype][name];
row.item_weight_price_total = row.item_weight * row.item_weight_price;
refresh_field(“items”);
});

Now it refreshes automatically, strange but it works. Thanks for your kindness.

@mrmo
if you use

frappe.model.set_value(cdt, cdn, "field_name", value);

then no need to refresh_field

1 Like

@kolate_sambhaji Could you please give a hint where to insert this

frappe.model.set_value(cdt, cdn, “field_name”, value);

Thanks

frappe.ui.form.on("Sales Order Item", "item_weight", function(frm, cdt, cdn) {
       var d = locals[cdt][cdn];
       frappe.model.set_value(cdt, cdn, "item_weight_price_total", d.item_weight * d.item_weight_price);
});
3 Likes

@jof2jc Thank you!

I’d like to do something extremely similar to this with Material Request Items, but I can’t find the file to insert this code.

Do you just insert the python into the Material Request doctype, or where?

@JamesE You insert it into the custom script which you can find in the Setup module under customize

Hello @Roy_Stephen, how did you sort our the refresh issue you have? I have the same problem.

Hi Ahmed

I never got it working perfectly and it is long but this was my last effort

I’m sure there’s a better way but I haven’t worked on it for months now.

Hope it helps

1 Like

Hi, guys. Thanks everybody in this thread for valuable discussion. Hope my case is not very off-topic here. I’ve modified Quality Inspection doctype so that it calculates repair rate with the help of following script:

frappe.ui.form.on("Quality Inspection", "repaired_qty", function(frm, cdt, cdn) {
       var d = locals[cdt][cdn];
       frappe.model.set_value(cdt, cdn, "repair_rate", d.repaired_qty * 100 / d.inspected_qty);
});

frappe.ui.form.on("Quality Inspection", "inspected_qty", function(frm, cdt, cdn) {
       var d = locals[cdt][cdn];
       frappe.model.set_value(cdt, cdn, "repair_rate", d.repaired_qty * 100 / d.inspected_qty);
});

Everything works great, but the problem is that I created repair_rate custom field as Data type and resulting numbers are shown like 10.256410256410257.
Is there any way to set precision in custom script? Or the only solution would be to create another custom field with Float type?

You can do this I guess

frappe.model.set_value(cdt, cdn, "repair_rate", flt(d.repaired_qty * 100 / d.inspected_qty, precision('repair_rate')));

flt(2.xxx, precision('fieldname')) should round it for you
3 Likes

Thank you, it worked!

I’m trying to calculate the total billing hrs for Sales Invoice, using this post: Sum fields amount From Child Table V7

But it doesn’t work…where is the problem please?

frappe.ui.form.on("Sales Invoice Timesheet", {
    billing_hours: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        var total = 0;
        frm.doc.timesheets.forEach(function(d) { total += d.billing_hours; });
        frm.set_value('total_hrs', total);
    }
});

Hello;
How I can access (get and set) the value of field in previous row of the changed row in the child table?
Regards
Bilal

Try this… Not sure if it helps
var d = locals[cdt][cdn]
var previous row = frm.doc.items[d.idx-1];

I try same code to change the rate of item in quotation but it doesnt work
pls reply

In Quotation, the item rate is automatically fetched when selecting the item code …what are you trying to accomplish?

Please explain thoroughly so we can help you