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);
}
Based on my limited understanding - this is what I have to offer.
By guess is, this code should work in Frappe CRM as it follows the standard approach for adding custom scripts in Frappe-based applications. Frappe CRM is built on the Frappe framework, so the same principles and methods apply.
Here are a few points to ensure smooth integration:
Custom Script Setup - Make sure your custom script is properly set up in the Frappe CRM environment. You can add your script through the Custom Script doctype in Frappe.
Form Script - Your script uses frappe.ui.form.on, which is the correct way to add custom behavior to forms in Frappe. This method is supported in Frappe CRM.
Dependencies - Ensure that any dependencies or custom fields (your_fieldname, custom_project_revenue, probability, custom_probability_value) are correctly defined in your DocType.
Console Logs - Add console.log statements in your script to print values and check the flow of execution. This helps identify where the script might be failing.
Inspect Elements - Use the browser’s developer tools to inspect elements and ensure that your custom buttons and fields are being rendered correctly.