Get_doc with Quality Inspection by item_code doesn't work

I’ve been trying to rebuild the Items table in Quotation (and then Sales Invoice) printing using Jinja templating. Particularly, in Print Format Builder, I replace the built-in Items Table by a new Custom HTML.

First attempt using hardcode string (naming_series field) works:
frappe.get_doc("Quality Inspection","QI-00003")

But then when I replace the expression with something meaningful such as:
frappe.get_doc("Quality Inspection",quotation_item.item_code)
frappe.get_doc("Quality Inspection","quotation_item.item_code")

So the question is can I use any other required key (such as item_code above) to get a submitted item in Quality Inspection?

Another thing is get_value always throws an exception when using with any kind of expression for filters which are described here: https://frappe.github.io/frappe/current/api/frappe.database.html. Might I get help from any one for some working examples?

Thanks,
Pop

Hi, Pop

frappe.get_doc(“Quality Inspection”,“quotation_item.item_code”)

This one is like giving “name” as a string “quotation_item.item_code” - it will work if you set this as a name for your doctype.

frappe.get_doc(“Quality Inspection”,quotation_item.item_code)

This one on the other hand is probably not a string (item_code). Try conversion

frappe.get_doc(“Quality Inspection”, str(quotation_item.item_code))

Btw - are you sure that item_code is the name field?

Update:
I checked and it has naming series, so item_code is probably nothing like “QI-00003” - cause QI stands for Quality Inspection, right?

Thanks for the very quick response. You are right for naming series. I am doubt that there is nothing to do with object type conversion but it has to with how get_doc actually works. Ideally I’d love to see if it can take item_code as a valid argument but it seems not.

Following is our default doctype of Quality Inspection.

@pop

get_doc(doctype, name_of_particular_record)

You should definitely go for filters in get_all.
Because you have to filter against particular field and not a name.

Please try:

frappe.get_all(‘Quality Inspection’, filters={‘item_code’ : quotation_item.item_code})
NOTE: Does not check for permissions - use get_list instead!

You may find useful:

And the cheatsheet:

1 Like

Hey @pop

Somehow you want to retrieve Quality Inspection from item_code you could do:
item_code = "whatever" name = frappe.get_value("Quality Inspection", filters={"item_code": item_code}) // Just return the first row which is matched doc = frappe.get_doc("Quality Inspection", name)

2 Likes

@phamviet

This one is classic solution - nice. Of course @pop you can use get_all as well to limit to 1 record only:

frappe.get_all(‘Quality Inspection’, filters={‘item_code’ : quotation_item.item_code}, limit_page_length = 1 )

1 Like

Very glad to get help from you guys @Marcin_Walkow and @phamviet. I will give it a try tomorrow and let you know how it goes. Thanks :wink:

An update for this issue: getting the same issue as yesterday whenever getting in use of ‘filters’.

Traceback (innermost last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 57, in application
    response = frappe.handler.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 19, in handle
    execute_cmd(cmd)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 36, in execute_cmd
    ret = frappe.call(method, **frappe.form_dict)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 806, in call
    return fn(*args, **newargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/templates/pages/print.py", line 142, in get_html_and_style
    no_letterhead=no_letterhead, trigger_print=trigger_print),
  File "/home/frappe/frappe-bench/apps/frappe/frappe/templates/pages/print.py", line 121, in get_html
    html = template.render(args, filters={"len": len})
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/./templates/print_formats/standard.html", line 28, in top-level template code
    {{ render_field(df, doc) }}
  File "/home/frappe/frappe-bench/apps/frappe/frappe/./templates/print_formats/standard_macros.html", line 5, in template
    
{{ frappe.render_template(df.options, {"doc": doc}) or "" }}

  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/jinja.py", line 50, in render_template
    return get_jenv().from_string(template).render(context)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "

So it seems like frappe doesn’t support filters on templating language such as Jinja2, am I correct? What I am trying to do is not touching anything to the logic but retrieving values from different tables as to then organize it for better printing layout. Any other hints would be highly appreciated.

From inside the Jinja2 it looks like it doesn’t understand syntax :octopus: