Query Contact Based on Customer and Customer Name

Hi,

I am trying to get the contact for the selected customer in a form but I am unable to find a way to do it with the recent changes in contact form.

Earlier the contact had a customer field and hence passing the filters was easy as in:

	onload: function(frm){
		frm.set_query("contact", function(doc) {
			return {
				"filters": {
					"customer": doc.customer
				}
			};

But now the customer field is pushed to a Child Table Dynamic Link and due to this I am unable to figure out how to get the contacts for the selected customer. Here is the screenshot of the form, in the child table of the form I have 2 field

  • document (which is a link field for DocType)
  • document_name (which is the name of the customer)

Now I am planning to pass the query as below:

frappe.ui.form.on('Sales Call Tool', {
	setup: function(frm) {
		frm.set_query("document", "details", function() {
			return {
				filters: [
					['name', 'in', ['Customer', 'Lead']]
				]
			};
		});
		frm.set_query("contact", "details", function() {
			return {query: "rigpl_erpnext.utils.query.contact_query"}
		});
	},
});
def contact_query(document, doc_name):
	return frappe.db.sql("""SELECT  cu.name FROM `tabContact` cu, `tabDynamic Link` dl
		WHERE dl.parent = cu.name AND dl.link_doctype = '%s' 
		AND dl.link_name = '%s'""" %(document, doc_name))

The issue is that I am getting an error:

Traceback (most recent call last):
  File "/home/aditya/frappe-bench/apps/frappe/frappe/app.py", line 57, in application
    response = frappe.handler.handle()
  File "/home/aditya/frappe-bench/apps/frappe/frappe/handler.py", line 22, in handle
    data = execute_cmd(cmd)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/handler.py", line 53, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/__init__.py", line 923, in call
    return fn(*args, **newargs)
  File "/home/aditya/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/aditya/frappe-bench/apps/frappe/frappe/desk/search.py", line 35, in search_widget
    searchfield, start, page_length, filters, as_dict=as_dict)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/__init__.py", line 923, in call
    return fn(*args, **newargs)
TypeError: contact_query() takes exactly 2 arguments (6 given)

Help on this would be great and pardon my programming knowledge.

@adityaduggal why not make this a standard feature and start a pull? That will make us more motivated to help you :wink:

I would love to do it now the issue is that where should I send the pull request.
I can easily figure out that contact_query should be a part of the frappe app but where to add is the question. If you could let me know I would definitely send a PR.

First share the specs, you can send pull to both!

If you are tracking calls, maybe it should be part of CRM. Depends on your functionality (which you should share first!)