Get internal_links with Jinja?

Let’s say we have a Sales Order (SO) and a Delivery Note (DN) with an internal_link. The SO Dashboard “Fulfillment” column links to the DN, and the DN Dashboard “Reference” column links to the SO. But, neither document has this link as an attribute, such that the linkage cannot be discovered using {% frappe.get_list() %}. How can this link be discovered in Jinja?

My use case is a custom print Delivery Note (DN) that includes the linked Sales Order (SO) name. A simple solution is to customize the DN DocType to explicitly include this SO link, but this approach seems redundant because the link already exists as an internal_link. Or, write and whitelist a custom Python method for Jinga to get the internal_links. Or, what, … I don’t know the best approach. Is this an unusual use case?

Thanks.

The link is present somewhere. It could be against each items. Check the item details in the table.

Yes, I see. I wrote a Jinja routine to print all attribute values, including child tables and linked documents. Tuneable parameters are links of interest and recursive loop depth.

<h2>{{ doc.name }}</h2>
{%
    set show_linked_doctypes = [
        'Sales Order',
    ]
%}
{% set recursion_depth = 5 %}
<ol>
    {% for item in [doc] recursive %}
        {% if loop.depth != recursion_depth %}
            {% set outer_loop = loop %}
            <li>name: {{ item.name }}</li>
            <ol>
                {% for field in frappe.get_meta(
                    item.doctype
                ).fields %}
                    {% set fieldname = field.fieldname %}
                    {% set fieldtype = field.fieldtype %}
                    {% set value = item[fieldname] %}
                    {% if value %}
                        {% if fieldtype in (
                            'Table',
                            'Table MultiSelect'
                        ) %}
                            <li>{{ fieldname }}:
                                <ol>{{ outer_loop(value) }}</ol>
                            </li>
                        {% elif
                            fieldtype == 'Link'
                            and
                            field.options in show_linked_doctypes
                        %}
                            <li>{{ fieldname }}: {{ value }}
                                <ol>{{ outer_loop(
                                    [ frappe.get_doc(
                                        field.options,
                                        value
                                    ) ]
                                ) }}</ol>
                            </li>
                        {% else %}
                            <li>{{ fieldname }}: {{ value }}</li>
                        {% endif %}
                    {% endif %}
                {% endfor %}
            </ol>
        {% endif %}
    {% endfor %}
</ol>
3 Likes

I spent hours looking for how to get linked values. This script is amazing. I am now able to see all available options to print!

Thank you so much! this is gold!