Filtering the Contact based on the Customer at the Timesheet

Hello;

I did some customization in the timesheet, so I added customer field and at the time_log table (which is a link for Timesheet Detail), I also did customization and added the Contact field. But I need the Contact field to be filtered based on the selected customer as shown in the below image:

I added the following code on the onload event for the timesheet.js but it did not work:

            frm.set_query("contact", "time_logs", function(doc, cdt, cdn) {
                    var item_selected = locals[cdt][cdn];
                    return {
                            filters: {
                                    'link_name': frm.doc.customer
                            }
                    };
            });

Well, maybe the main reason for the problem that the Contact doctype has the Table configured as Dynamic Link as shown in the below images:

And below is the Contact doctype which contains the Table links to the Dynamic Link:

How can I do this filter?

Thanks
Regards
Bilal

Try below code

return {
	query: 'frappe.contacts.doctype.contact.contact.contact_query',
	filters: {
		link_doctype: 'Customer',
		link_name: frm.doc.customer
	}
};

2 Likes

Hello @rohit_w
Thank you.
I tried your code and I got the error:

link_name = filters.pop('link_name')
KeyError: u'link_name'

In the below Traceback:

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 62, in application
response = frappe.handler.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 22, in handle
data = execute_cmd(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 53, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 939, in call
return fn(*args, **newargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/desk/search.py”, line 14, in search_link
search_widget(doctype, txt, query, searchfield=searchfield, page_length=page_length, filters=filters)
File “/home/frappe/frappe-bench/apps/frappe/frappe/desk/search.py”, line 35, in search_widget
searchfield, start, page_length, filters, as_dict=as_dict)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 939, in call
return fn(*args, **newargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/contacts/doctype/contact/contact.py”, line 127, in contact_query
link_name = filters.pop(‘link_name’)
KeyError: u’link_name’

Also I tried to place 'link_name' instead of link_name, but the same error !

Regards
Bilal

Seems link_name is blank, did you selected customer?

Hello @rohit_w

100% You are right. The problem was in the name of the customer field, I added it by mistake as cutomer instead of customer.

Thank you a lot. It worked fine.

Regards
Bilal