Get Items from -> Delivery Note -> selecting filters gives SQL Syntax Error

“erpnext”: “13.36.2”,
“frappe”: “13.36.3”

Route

Form/Sales Invoice/new-sales-invoice-1

Trackeback

Syntax error in query:
select `tabDelivery Note`.name, `tabDelivery Note`.title, `tabDelivery Note`.customer, `tabDelivery Note`.posting_date, `tabDelivery Note`.status, `tabDelivery Note`.customer_name, `tabDelivery Note`.territory, `tabDelivery Note`.base_grand_total
		from `tabDelivery Note`
		where `tabDelivery Note`.`name` like %(txt)s and
			`tabDelivery Note`.docstatus = 1
			and status not in ("Stopped", "Closed")  and `tabDelivery Note`.`docstatus` = 1.0 and `tabDelivery Note`.`company` = 'my company' and `tabDelivery Note`.`is_return` = 0.0 and `tabDelivery Note`.`customer` = 'my customer' and `tabDelivery Note Item`.`item_code` = ['is', 'set']
			and (
				(`tabDelivery Note`.is_return = 0 and `tabDelivery Note`.per_billed < 100)
				or (`tabDelivery Note`.grand_total = 0 and `tabDelivery Note`.per_billed < 100)
				or (
					`tabDelivery Note`.is_return = 1
					and return_against in (select name from `tabDelivery Note` where per_billed < 100)
				)
			)
			 order by `tabDelivery Note`.`name` asc limit 0, 25
Traceback (most recent call last):
  File "apps/frappe/frappe/app.py", line 69, in application
    response = frappe.api.handle()
  File "apps/frappe/frappe/api.py", line 55, in handle
    return frappe.handler.handle()
  File "apps/frappe/frappe/handler.py", line 38, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 76, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1457, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/desk/search.py", line 126, in search_widget
    raise e
  File "apps/frappe/frappe/desk/search.py", line 114, in search_widget
    frappe.response["values"] = frappe.call(
  File "apps/frappe/frappe/__init__.py", line 1457, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/desk/search.py", line 294, in validate_and_sanitize_search_inputs
    return fn(**kwargs)
  File "apps/erpnext/erpnext/controllers/queries.py", line 371, in get_delivery_notes_to_be_billed
    return frappe.db.sql(
  File "apps/frappe/frappe/database/database.py", line 174, in sql
    self._cursor.execute(query, values)
  File "env/lib/python3.8/site-packages/pymysql/cursors.py", line 148, in execute
    result = self._query(query)
  File "env/lib/python3.8/site-packages/pymysql/cursors.py", line 310, in _query
    conn.query(q)
  File "env/lib/python3.8/site-packages/pymysql/connections.py", line 548, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "env/lib/python3.8/site-packages/pymysql/connections.py", line 775, in _read_query_result
    result.read()
  File "env/lib/python3.8/site-packages/pymysql/connections.py", line 1156, in read
    first_packet = self.connection._read_packet()
  File "env/lib/python3.8/site-packages/pymysql/connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "env/lib/python3.8/site-packages/pymysql/protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "env/lib/python3.8/site-packages/pymysql/err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '['is', 'set']\n\t\t\tand (\n\t\t\t\t(`tabDelivery Note`.is_return = 0 and `tabDelivery...' at line 5")

Request Data

{
	"type": "GET",
	"args": {
		"doctype": "Delivery Note",
		"txt": "",
			"filters": "{\"docstatus\":1,\"company\":\"my company\",\"is_return\":0,\"customer\":\"my customer\",\"item_code\":[\"is\",\"set\"]}",
		"filter_fields": "[\"customer\"]",
		"page_length": 25,
		"query": "erpnext.controllers.queries.get_delivery_notes_to_be_billed",
		"as_dict": 1
	},
	"headers": {},
	"error_handlers": {},
	"url": "/api/method/frappe.desk.search.search_widget"
}

Response Data

{
	"exception": "pymysql.err.ProgrammingError: (1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '['is', 'set']\\n\\t\\t\\tand (\\n\\t\\t\\t\\t(`tabDelivery Note`.is_return = 0 and `tabDelivery...' at line 5\")"
}

Any help would be greatly appreciated!

@TheOnlineExplorer From the traceback, you can see the following line has syntax issue since we only use = for a single comparison and IN for multiple. Thats why it throw syntax error

May I know what you trying to filter for item_code?

Thanks for replying and thanks for explaining the difference between ‘=’ and ‘IN’.
Yes, I’m trying to filter item_code!
So, are there any changes you recommend? Or alternate approach to filter item_code?
Thank you very much.

If u were trying to filter item_codemore than 1, then you could try this (haven’t tested)>

"item_code": ["in", ["item_code_1", "item_code_2"]]
1 Like

I’ll definitely try that suggestion. I can see myself filtering multiple (at least 2 item_code) in the future!
But no, I was just trying to filter only 1 item_code this time.

Understood, the above code u can use to filter single or multiple depending on ur need by removing the values. Try it out and let me know the outcome. Thanks