I have made a working code to calculate a value in Frappe CRM. But this event needs to be triggered with a button. How to make the function triggered every time the status/field is change in Frappe CRM?
// Function to set up the form with actions
function setupForm({ doc, call, $dialog, updateField, createToast }) {
return {
actions: [
{
label: "Update Probability Value",
onClick: function() {
let revenue = doc.custom_project_revenue;
let probability = doc.probability;
let probabilityValue = revenue * probability / 100;
// Update the custom probability value field
updateField("custom_probability_value", probabilityValue);
}
}
],
};
}
frappe.ui.form.on('Your DocType', {
refresh: function(frm) {
// Hide the button initially
frm.remove_custom_button('Update Probability Value');
},
your_fieldname: function(frm) {
// Show the button when the specific field is changed
if (frm.doc.your_fieldname) {
frm.add_custom_button('Update Probability Value', function() {
let revenue = frm.doc.custom_project_revenue;
let probability = frm.doc.probability;
let probabilityValue = revenue * probability / 100;
// Update the custom probability value field
frm.set_value('custom_probability_value', probabilityValue);
});
} else {
frm.remove_custom_button('Update Probability Value');
}
},
// Trigger the function when the specific field is changed
custom_project_revenue: function(frm) {
updateProbabilityValue(frm);
},
probability: function(frm) {
updateProbabilityValue(frm);
}
});
function updateProbabilityValue(frm) {
let revenue = frm.doc.custom_project_revenue;
let probability = frm.doc.probability;
let probabilityValue = revenue * probability / 100;
// Update the custom probability value field
frm.set_value('custom_probability_value', probabilityValue);
}