Skip to main content

Information From Another Document In Print Format

 

Introduction: What Does Getting Information from Another Document Mean?

In Frappe Framework v15, print formats are rendered using Jinja templates. Often, business documents such as Sales Invoices, Purchase Orders, or Delivery Notes need to display information that belongs to another related DocType.

Examples include:

  • Showing Customer details on a Sales Invoice
  • Printing Item metadata from Item master
  • Displaying linked Address or Contact information

Frappe provides safe, framework-supported methods to fetch and render such related data inside print formats.

Why Fetch Data from Another DocType in Print Formats?

Fetching related document data is required to:

  • Avoid data duplication in DocTypes
  • Keep print formats dynamic and accurate
  • Ensure updates in master data reflect automatically
  • Maintain clean database design

Frappe v15 supports this without custom Python code, directly within print format templates.

How Print Formats Work in Frappe v15

Print formats in Frappe:

  • Use Jinja templating
  • Have access to the current document via doc
  • Can safely call limited framework utilities
  • Are rendered server-side

The current document (e.g., Sales Invoice) is always available as doc.

How to Access Linked Document Fields Directly

When Can You Access Fields Directly?

If a field is a Link field and the linked value is already loaded, you can access it directly.

Example: Accessing Customer Name

{{ doc.customer_name }}

This works only if the field exists on the current DocType.

How to Fetch Data from Another DocType in Print Format

Recommended Method: frappe.get_doc()

Answer:
Use frappe.get_doc() to fetch another document safely inside a print format.

Example: Fetch Customer Document

{% set customer = frappe.get_doc("Customer", doc.customer) %}
{{ customer.customer_group }}

This retrieves the full Customer document linked to the current document.

Fetching Specific Fields Using frappe.db.get_value()

When to Use This Method

Use frappe.db.get_value() when:

  • You need only one or two fields
  • Performance is important
  • You want minimal database access

Example: Fetch Customer Group

{{ frappe.db.get_value("Customer", doc.customer, "customer_group") }}

This method is efficient and recommended for simple lookups.

Fetching Child Table Data from Another Document

Example: Get Address Details

{% set address = frappe.get_doc("Address", doc.customer_address) %}
{{ address.address_line1 }}
{{ address.city }}

This is commonly used for:

  • Billing Address
  • Shipping Address
  • Contact details

Using Conditional Logic While Fetching External Data

Example: Safe Conditional Rendering

{% if doc.customer %}
{{ frappe.db.get_value("Customer", doc.customer, "territory") }}
{% endif %}

This avoids rendering errors when linked fields are empty.

Best Practices for Fetching Data in Print Formats

Prefer frappe.db.get_value() for simple fields
It reduces memory usage and improves render performance.

Avoid loops with database calls
Never call database queries inside large Jinja loops.

Use variables for reuse
Store fetched documents in variables to avoid repeated queries.

Keep print formats read-only
Do not perform updates or writes in print formats.

Common Mistakes and Troubleshooting

Print format fails to render

  • Ensure the linked field is not empty
  • Validate DocType and field names
  • Avoid calling unsupported Python methods

Performance issues

  • Replace frappe.get_doc() with frappe.db.get_value() where possible
  • Avoid fetching entire documents unnecessarily

Field not showing value

  • Confirm the field exists on the target DocType
  • Check field permissions and visibility

Advanced Use Case: Fetching Item Master Data in Sales Invoice

{% for item in doc.items %}
{{ frappe.db.get_value("Item", item.item_code, "item_group") }}
{% endfor %}

This allows printing master-level attributes without duplicating fields in transaction documents.

Industry Relevance and Use Cases

This feature is critical in:

  • ERPNext invoice and order printing
  • Manufacturing BOM and job card formats
  • Logistics and dispatch documents
  • Compliance and statutory printouts

It ensures data accuracy and centralized control.

Technical Prerequisites

  • Frappe Framework Version 15
  • Print Format access permissions
  • Correct Link field configuration
  • Basic understanding of Jinja syntax

Conclusion

Getting information from another document in print formats is a powerful and essential feature in Frappe Framework v15. By using supported APIs like frappe.get_doc() and frappe.db.get_value(), developers can create dynamic, accurate, and maintainable print formats—without compromising performance or data integrity.
For ERPNext and Frappe implementations, mastering this technique is fundamental to professional-grade document output.

Rating: 5 / 5 (2 votes)