How to Add a Dynamic Filter for a Link Field in Frappe/ERPNext

Hi all,

I’m trying to implement a dynamic filter for a Link field in a child table (grid) in Frappe/ERPNext, but I’m a bit stuck. I want the options in the Link field to change dynamically based on other fields or already selected values in the same table.

For example:

  • I have a child table called sprint_backlog_management.
  • Each row has a Link field backlog pointing to Backlog Management.
  • I want the backlog dropdown to exclude backlogs that have already been selected in other rows.
  • Ideally, if a row is removed or edited, the options in other rows should update automatically.

I know I need to use something like get_query on the field, but I’m not sure about the best way to implement it safely, especially for child table rows that may not exist yet when the form loads.

Could anyone guide me on the proper approach or provide an example of how to:

  1. Get all the currently selected values in the table.
  2. Apply a dynamic filter to a Link field in each row.
  3. Handle updates when rows are added, edited, or deleted.

Thanks in advance!

frappe.ui.form.on(“Parent Doctype”, {
// refresh(frm) {

// },

});
// Child Table Name
frappe.ui.form.on(‘Sprint Backlog Management’, {
backlog: function(frm){
frm.fields_dict[‘sprint_backlog_management’].grid.get_field(‘backlog’).get_query = function(doc){
var backlogs_list = ;
$.each(doc.sprint_backlog_management, function(idx, val){
if (val.backlog) backlogs_list.push(val.backlog);
});
return { filters: [[‘Backlog Management’, ‘name’, ‘not in’, backlogs_list]] };
};
}
}); with the help of this code - backlog dropdown to exclude backlogs that have already been selected in other rows.

2 Likes

thanks a lot broo

1 Like