Issue with using set query to filter a link field

I added a child table in Sales Order doctype called Container List which captures a list of containers related to each sales order. From the Sales Order, a Project connected to the Sales Order is created and in this, a user can create multiple tasks in relation to the delivery of these Containers. To achieve this, I would want the linked field for Container No. to be filtered to only show the containers which we earlier created in the Container List child table in Sales Order.

I have written this client script and I can see that the sales_order is being picked, however the filter is not being applied.

frappe.ui.form.on('Task', {
    refresh: function(frm) {
        // Check if the Project has a Sales Order linked to it
        if (frm.doc.project) {
            frappe.call({
                method: 'frappe.client.get_value',
                args: {
                    doctype: 'Project',
                    filters: { name: frm.doc.project },
                    fieldname: 'sales_order'
                },
                callback: function(response) {
                    var sales_order = response.message.sales_order;
                    console.log(sales_order);
                    if (sales_order) {
                        frm.set_query('custom_container_number_ref', function() {
                            return {
                                filters: {
                                    parent: sales_order
                                }
                            };
                        });
                    }
                }
            });
        }
    }
});

Any help would be greatly appreciated.

@41sMusah please check this link.

Usually we do not link to child table rows, but to standalone documents. So you might need a bit of a workaround here.

If your containers are standalone documents, you can fetch the list of containers that are linked in the sales order and set the query to only allow these values in the Task’s link field.

If your containers are just rows, not individual documents, then you can use a “Select” field in Task and set the field’s options according to the container numbers in the Sales Order.

1 Like