Skip to content

Instantly share code, notes, and snippets.

@esafwan
Created April 28, 2024 20:31
Show Gist options
  • Save esafwan/9bd86519d90618e8d55493e980868d85 to your computer and use it in GitHub Desktop.
Save esafwan/9bd86519d90618e8d55493e980868d85 to your computer and use it in GitHub Desktop.
Override frappe call to intercept the call and act on basis of it.
/**
* Extends the functionality of Frappe's assignment feature for Leads.
*
* Problem:
* When a document like a Lead is assigned to a user via the sidebar, the information about the assignment
* (who it was assigned to, who assigned it, and when) is not stored in a way that can be easily displayed
* on Kanban cards or list columns. It only results in a ToDo list item without these details on the Lead itself.
*
* Solution:
* We are customizing frappe.call to intercept calls related to assignment actions. By overriding these calls,
* we can capture necessary data and store it in custom fields on the Lead doctype. This allows displaying the
* assignment information directly in the Lead's views (like Kanban or list views), enhancing visibility and
* traceability.
*
* Strategy:
* 1. Backup the original frappe.call to maintain standard functionality when needed.
* 2. Override frappe.call to inject custom logic when assignment-related methods are invoked.
* 3. Depending on the action (assign, unassign, close), update the Lead's custom fields to reflect these changes.
*/
frappe.ui.form.on('Lead', {
setup: function(frm) {
// Backup of the original frappe.call to use after custom logic
const originalCall = frappe.call;
// Override the standard frappe.call with customized behavior for assignment methods
frappe.call = function(opts) {
switch (opts.method) {
case "frappe.desk.form.assign_to.add":
console.log("Assigned to:", opts.args.assign_to);
// Custom logic to add assignment info to a Lead-specific custom field
//frm.set_value('custom_assigned_to', opts.args.assign_to);
//frm.save();
break;
case "frappe.desk.form.assign_to.remove":
console.log("Unassigned from:", opts.args.assign_to);
// Custom logic to clear or update the assignment info in Lead's custom field
//frm.set_value('custom_assigned_to', '');
//frm.save();
break;
case "frappe.desk.form.assign_to.close":
console.log("Assignment closed for:", opts.args.assign_to);
// Additional logic to handle closing of assignments if necessary
break;
default:
console.log("Operation not recognized", opts);
}
// Continue with the original frappe.call function to ensure normal operation
return originalCall(opts);
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment