Set filter on link field on change of value in other link field in child table

I have both project and task in timesheet details
I would like to filter the task in the the task link based on what has been selected in the project field.

I have this code which is triggered on a change in the project field
I can see that the change correctly runs the triger in my console but the code seems to have no effect on the filter of the task field

frappe.ui.form.on(“Timesheet Detail”, “project”, function (frm, cdt, cdn) {
var timesheet_detail = locals[cdt][cdn];
console.log(“Timesheet Detail project task filter=” + timesheet_detail.project);
if (timesheet_detail.project) {
frm.fields_dict[‘time_logs’].grid.get_field(‘task’).get_query = function () {
return {
filters: {
“project”: timesheet_detail.project,

            }
        };
    };
}

});

I have also tried it like this
frm.set_query(“task”, “time_logs”, function (doc, cdt, cdn) {
return {
filters: {
“project”: timesheet_detail.project,

            }
        };
    });

Again with no effect.
I know the filter works because if I use it in the forms onload or refresh I can see the filtering on the task field

Is there a function similar to refresh_field for link filters. Do they have to be applied in some way?

I am going to show my solution because I can see many questions in the forum over this subject.
Basically my code is correct. It works but only if it is applied to the link field before the form appears.
I have not yet found the function that causes it to apply but my solution is as follows

1)In the parent doctypes onload or refresh function i do this

frm.set_query(“task”, “time_logs”, function (doc, cdt, cdn) {
return {
filters: {
“project”: frappe. my_global_project_name_to_filter_against;
}
};
});

2)Then in my trigger function
frappe.ui.form.on(“Timesheet Detail”, “project”, function (frm, cdt, cdn) {
var timesheet_detail = locals[cdt][cdn];
//declare a variable in the frappe namespace
frappe. my_global_project_name_to_filter_against = timesheet_detail.project;

This technique works reliably.

I have been going through the frappe code to find the function that is called to initially set the filter for the link field but I have not found it yet, My technique works and hopefully helps someone trying to solve the same problem.

1 Like

This works for me.

frappe.ui.form.on('Timesheet', {
	setup(frm) {
	    frm.set_query('task', 'time_logs', function(doc, cdt, cdn) {
            let d = locals[cdt][cdn];
            return{
				filters: {
				    'project': d.project,
				    'status': ['Not In', 'Cancelled,Template'],
					'department': doc.department
				}
			};
	    });
	}
});

Thanks for that.
Your code will work in onload refresh etc…
The problem is that will not work when triggered by a field change.
Try it and you will see.
My solution sets an outside variable that I can reference and change. This seems to work.
I would like to know how to apply filter changes to a field after the form is rendered instead of resorting to this trick.