Fetch custom field of a custom doctype in sales invoice print format

Hey everyone,

couldn’t find a similar topic in the forum search, so I hope someone can help me with this.
Currently I’m running ERPNext on version 14 with a nearly default setup.

One adjustment I needed to make, besides a custom print format for my sales quotations and invoices, was to add the possibility for an intro and outro text on my quotations and invoices (just one or a few sentences before and after the positions list).

To have it a little bit more standardized I decided to save create my own simple custom doctypes which should contain the text phrases, so I can easily reuse them again.
These doctypes are really simple and just contain the following data(types) - for now I will refere to the intro:

  • intro_name (data)
  • intro_text (short text)
  • naming series (INV-INTRO-{intro_text})

After that I linked these doctype fields into the standard sales invoice doctype (inv_intro_text) which is working already (type: link, option: custom intro doctype, fetch from: intro_text).
Unfortunately this results into the situation that the custom field inside the sales invoice does contain just the naming series after the selection and when I’m trying to fetch from this field in my print format ({{doc.inv_intro_text}}) it will just enter the naming series instead of the actual short text (which kinda makes sense…).

At this point I’m currrently struggeling - I tried out a few things, but nothing did solve my problem.

  1. First thing I tried was to catch the “intro_text” directly out of the doctype:
{% set my_intro_text = frappe.get_doc("Sales Invoice Intro Text", intro_text) %}

{{ my_intro_text.intro_text }}

At first it looked promising, because the only issue was that the text was not formated, but later I recognized that it will only take the first data set out of the doctype - so, no my selection inside the sales invoice did not matter at all.

  1. The second thing I tried was to add an additional related field (intro_display) inside the sales invoice which contains the actual “intro_text” value/short text. (type: link, option: custom intro doctype, fetch from: custom intro doctype.intro_text). And my plan was to fetch this field afterwards inside my print format.
    At first it seemed to work and the short text was displayed (formated!) inside the new field, but when I tried to save the changes I got the message that “intro_field could not be found”.

Unfortunately I’m out of ideas now and I don’t know how to resolve one of these issues, because I’m still new to the ERPNext world…
I would say that fetching the “intro_text” directly for the print format would be the easiest way, but I can’t work it out…
Has someone any advice or idea?

Hi @Nicolai_Knr,

To fetch the “intro_text” directly in your print format.
you can modify your code for check it.

{% set my_intro_text = frappe.get_doc("Sales Invoice Intro Text", doc.inv_intro_text) %}
{{ my_intro_text.intro_text }}

In above code, doc.inv_intro_text refers to the selected value in the “inv_intro_text” field of the current sales invoice.
By passing this value to frappe.get_doc(), you can fetch the corresponding “Sales Invoice Intro Text” document and access its “intro_text” field using my_intro_text.intro_text.

You save your changes and test the print format again. This modification should display the selected “intro_text” in your print format.

Please check your doctype name and field name according.

Thank You!

Hi @NCP,

you are amazing!
It’s working like it should - thank you!

One additional question:
Is it possible in this way of doing it to catch the short text in a formatted way aswell?
I tried something like

{{ doc.get_formatted('my_intro_text.intro_text') }}

But this let’s the text disappear…

Hi @Nicolai_Knr,

Use it.

{{ my_intro_text.get_formatted('intro_text') }}

Then reload and check it.

Thank You!

Hi @NCP,

thank you so much for all your advice and your time!

It’s working perfectly fine now and i think i understood the syntax a little bit better aswell.

Thank you!