Script Report TypeError: expected string or bytes-like object

Hello Everyone

I’m currently learning how to implement a script report. I found the documentation for it, but instead I used an existing report of a colleague to implement my own version. I started off fairly simple and wanted to just have an output of all items (which of course, can be done otherwise easily, but it’s just for testing purposes).

My code looks like this:

# Copyright (c) 2013, libracore AG and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe

def execute(filters=None):
        columns, data = [], []
        columns = ["Item Code:Link/Item:200"]

        if filters.item:
                item = filters.item
        else:
                item = "%"
        sql_query = "SELECT name FROM `tabItem`;".format(item=item) 
        data = frappe.db.sql(sql_query, as_list = True)
        return columns, data

It returns the following error:

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 60, in application
    response = frappe.api.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/api.py", line 55, in handle
    return frappe.handler.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 21, in handle
    data = execute_cmd(cmd)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 56, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1036, in call
    return fn(*args, **newargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 511, in wrapper_fn
    retval = fn(*args, **get_newargs(fn, kwargs))
  File "/home/frappe/frappe-bench/apps/frappe/frappe/desk/query_report.py", line 200, in run
    result = generate_report_result(report, filters, user)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/desk/query_report.py", line 75, in generate_report_result
    res = frappe.get_attr(method_name)(frappe._dict(filters))
  File "/home/frappe/frappe-bench/apps/repairs/repairs/repairs/report/lieferanten_artikelnummer/lieferanten_artikelnummer.py", line 15, in execute
    sql_query = "SELECT name FROM `tabItem`;".format(item=item)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/database/database.py", line 117, in sql
    if re.search(r'ifnull\(', query, flags=re.IGNORECASE):
  File "/home/frappe/frappe-bench/env/lib/python3.5/re.py", line 173, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

Despite the stacktrace, the error is not necessarily line 15. I could commentate that line or just leave it empty - it always pertains to line 15.

Hi,
Try it like

def execute:
columns, data = , get_data(filters)
columns = [“Item Code:Link/Item:200”]
return columns, data

def get_data(filters):
conditions = " 1 = 1"
if filters.get(‘item’):
conditions += " and name = ‘{0}’".format(filters.get(‘item’))

frappe.db.sql(“”" SELECT name FROM tabItem where {} “”".format(conditions),as_list=1)

Dont write your query in execute itself use different function and call it from execute.

1 Like

This… works. I’m confused because I don’t know why this works, but it does. No results currently show up, but the error is gone at least. Thank you