I have created a custom field for calculating the Unit Production Cost of a BOM. I have a client script that triggers every time the Total Cost is updated on the form.
Everything works fine, except when the Total Cost is updated by the "Update Cost’ button or automatically through a scheduled task.
How can I update my custom field every time the Total Cost is updated for the BOM, whether by a custom button action or as a scheduled task?
You can use to update your custom field for calculating the Unit Production Cost of a BOM:
frappe.provide("frappe.model");
frappe.ui.form.on("BOM", {
// This function is called whenever the Total Cost field is updated
total_cost: function(frm, cdt, cdn) {
// Get the total cost and quantity from the BOM
var total_cost = frm.doc.total_cost;
var qty = frm.doc.qty;
// Calculate the unit production cost
var unit_production_cost = total_cost / qty;
// Set the value of your custom field
frappe.model.set_value(cdt, cdn, "unit_production_cost", unit_production_cost);
}
});
With this script, your custom field for the Unit Production Cost should be updated automatically every time the Total Cost field is updated, whether it’s done manually or through a scheduled task.
If you set the code in the Update Code button, the base functionally related script will not work.
So I suggest that you, please add a custom button on the page and apply the script your according.