<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fawa Alhafiz on Medium]]></title>
        <description><![CDATA[Stories by Fawa Alhafiz on Medium]]></description>
        <link>https://medium.com/@fawaztwix111?source=rss-be601bc5a5d7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*5E6F1yEoV7IXF6_l</url>
            <title>Stories by Fawa Alhafiz on Medium</title>
            <link>https://medium.com/@fawaztwix111?source=rss-be601bc5a5d7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 23:28:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fawaztwix111/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Link vs Dynamic Link in Frappe/ERPNext: The Field Type That Will Change How You Build ERPs]]></title>
            <link>https://medium.com/@fawaztwix111/link-vs-dynamic-link-in-frappe-erpnext-the-field-type-that-will-change-how-you-build-erps-baca3f0b8afd?source=rss-be601bc5a5d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/baca3f0b8afd</guid>
            <category><![CDATA[report]]></category>
            <category><![CDATA[erpnext]]></category>
            <category><![CDATA[frappe]]></category>
            <dc:creator><![CDATA[Fawa Alhafiz]]></dc:creator>
            <pubDate>Mon, 01 Dec 2025 09:48:02 GMT</pubDate>
            <atom:updated>2025-12-01T09:50:34.578Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>A practical guide to understanding one of Frappe’s most powerful — yet often misunderstood — field types</em></blockquote><p>If you’ve been developing with Frappe or ERPNext, you’ve likely encountered both <strong>Link </strong>and <strong>Dynamic Link</strong> fields. At first glance, they seem similar — both create relationships between documents. But understanding when to use each one can be the difference between rigid, hard-coded solutions and truly flexible, maintainable applications.</p><p>Let me show you the real difference through a problem I recently solved.</p><h3><strong>The Real-World Problem</strong></h3><p>I was enhancing ERPNext’s Stock Ledger report to show related invoices. Here’s the challenge:</p><p>- <strong>Delivery Notes</strong> can have related <strong>Sales Invoices</strong></p><p>- <strong>Purchase Receipts</strong> can have related <strong>Purchase Invoices</strong></p><p>- Other voucher types (Stock Entry, Stock Reconciliation) don’t have invoices</p><p>I needed <strong>one column </strong>that could intelligently link to different document types. Enter Dynamic Link.</p><p><strong>Link Fields: The Single-Target Relationship</strong></p><p>A <strong>Link field</strong> is straightforward — it points to exactly one DocType, always.</p><pre>{<br>&quot;label&quot;: &quot;Customer&quot;,<br>&quot;fieldname&quot;: &quot;customer&quot;,<br>&quot;fieldtype&quot;: &quot;Link&quot;,<br>&quot;options&quot;: &quot;Customer&quot; # Always points to Customer DocType<br>}</pre><p><strong>When to Use Link Fields</strong></p><p>✅ The relationship is <strong>always</strong> to the same DocType</p><p>✅ You know the target DocType at design time</p><p>✅ Example: Sales Invoice → Customer (always Customer, never Supplier)</p><h3><strong>The Problem with Link Fields for My Use Case</strong></h3><p>If I had used a Link field for the invoice column:</p><pre>{<br>&quot;label&quot;: &quot;Invoice&quot;,<br>&quot;fieldname&quot;: &quot;invoice_no&quot;,<br>&quot;fieldtype&quot;: &quot;Link&quot;,<br>&quot;options&quot;: &quot;Sales Invoice&quot; # ❌ WRONG - only works for Sales Invoices!<br>}</pre><p><strong>What breaks:</strong></p><p>- ❌ Purchase Invoice numbers wouldn’t be clickable</p><p>- ❌ Clicking Purchase Invoices would try opening them as Sales Invoices → Error</p><ul><li>❌ Zero flexibility for different document types</li></ul><h3><strong>Dynamic Link: The Polymorphic Solution</strong></h3><p>A <strong>Dynamic Link</strong> field points to different DocTypes based on another field’s value.</p><pre>{<br>&quot;label&quot;: &quot;Invoice&quot;,<br>&quot;fieldname&quot;: &quot;invoice_no&quot;,<br>&quot;fieldtype&quot;: &quot;Dynamic Link&quot;,<br>&quot;options&quot;: &quot;invoice_type&quot; # ← Points to ANOTHER FIELD, not a DocType<br>}<br># The companion field that determines the target DocType<br>{<br>&quot;label&quot;: &quot;Invoice Type&quot;,<br>&quot;fieldname&quot;: &quot;invoice_type&quot;,<br>&quot;fieldtype&quot;: &quot;Data&quot; # Contains &quot;Sales Invoice&quot; or &quot;Purchase Invoice&quot;<br>}</pre><p><strong>How It Works</strong></p><p>At runtime, the system checks the <strong><em>invoice_type </em></strong>field:</p><p>- If <strong><em>invoice_type </em>= “Sales Invoice”</strong> → <strong><em>invoice_no</em></strong> links to Sales Invoice</p><p>- If <strong><em>invoice_type </em>= “Purchase Invoice”</strong> → <strong><em>invoice_no</em></strong> links to Purchase Invoice</p><p><strong><em>The magic:</em></strong> One field, infinite flexibility.</p><h3><strong>My Implementation in Stock Ledger Report</strong></h3><p>Here’s how I implemented this in the ERPNext Stock Ledger report:</p><pre>def get_columns(filters):<br>columns = [<br># … other columns …<br># The Dynamic Link column<br>{<br>&quot;label&quot;: _(&quot;Invoice&quot;),<br>&quot;fieldname&quot;: &quot;invoice_no&quot;,<br>&quot;fieldtype&quot;: &quot;Dynamic Link&quot;,<br>&quot;options&quot;: &quot;invoice_type&quot;, # Dynamic - determined at runtime<br>&quot;width&quot;: 120,<br>},<br>{<br>&quot;label&quot;: _(&quot;Invoice Date&quot;),<br>&quot;fieldname&quot;: &quot;invoice_date&quot;,<br>&quot;fieldtype&quot;: &quot;Date&quot;,<br>&quot;width&quot;: 100,<br>},<br>]<br>return columns</pre><p>Then when processing each stock ledger entry:</p><pre># For Delivery Notes<br>sle.update({<br>&quot;invoice_no&quot;: &quot;SINV-00001&quot;,<br>&quot;invoice_date&quot;: &quot;2024–01–15&quot;,<br>&quot;invoice_type&quot;: &quot;Sales Invoice&quot; # ← Tells Dynamic Link where to point<br>})<br># For Purchase Receipts<br>sle.update({<br>&quot;invoice_no&quot;: &quot;PINV-00001&quot;,<br>&quot;invoice_date&quot;: &quot;2024–01–16&quot;,<br>&quot;invoice_type&quot;: &quot;Purchase Invoice&quot; # ← Different target, same field<br>})</pre><ul><li><strong><em>Result</em>:</strong> One “Invoice” column that intelligently links to different invoice types based on the transaction type. Clean, flexible, maintainable.</li></ul><h3><strong>Common Dynamic Link Patterns in ERPNext</strong></h3><p>Once you understand Dynamic Link, you’ll see it everywhere in ERPNext:</p><p><strong>1. Party Type + Party</strong></p><p>Used in Payment Entries, Journal Entries, etc.</p><pre>party_type = &quot;Customer&quot; # or &quot;Supplier&quot;, &quot;Employee&quot;, etc.<br>party = &quot;CUST-001&quot; # Links to whatever party_type says</pre><p><strong>2. Voucher Type + Voucher No</strong></p><p>Used in Stock Ledger Entry, GL Entry, etc.</p><pre>voucher_type = &quot;Sales Invoice&quot; # or &quot;Delivery Note&quot;, &quot;Purchase Receipt&quot;<br>voucher_no = &quot;SINV-001&quot; # Links to the right DocType</pre><p><strong>3. Reference Type + Reference Name</strong></p><p>Used in comments, attachments, versions</p><pre>reference_doctype = &quot;Sales Order&quot;<br>reference_name = &quot;SO-001&quot; # Dynamically links to Sales Order SO-001</pre><h3><strong>The Decision Tree</strong></h3><p>Here’s how to choose between Link and Dynamic Link:</p><pre>Do you know the target DocType at design time?<br>│<br>├─ YES, it&#39;s always the same<br>│ └─ Use Link field<br>│<br>└─ NO, it could be different DocTypes<br>└─ Use Dynamic Link field</pre><h3><strong>Common Pitfalls to Avoid</strong></h3><p><strong>❌ Pitfall 1: Forgetting the Companion Field</strong></p><pre># WRONG - Dynamic Link without the target field<br>{<br>&quot;fieldtype&quot;: &quot;Dynamic Link&quot;,<br>&quot;options&quot;: &quot;invoice_type&quot; # ← invoice_type field doesn&#39;t exist!<br>}</pre><p><strong>Fix:</strong> Always create the companion field that Dynamic Link references.</p><p><strong>❌ Pitfall 2: Using Link When You Need Flexibility</strong></p><pre># WRONG - Hard-coding when requirements might change<br>{<br>&quot;fieldtype&quot;: &quot;Link&quot;,<br>&quot;options&quot;: &quot;Customer&quot; # What if you need Supplier later?<br>}</pre><p><strong>Fix:</strong> If there’s ANY chance you’ll need flexibility, use Dynamic Link from the start.</p><p><strong>❌ Pitfall 3: Using Dynamic Link Unnecessarily</strong></p><pre># OVERKILL - Customer is ALWAYS Customer<br>{<br>&quot;fieldtype&quot;: &quot;Dynamic Link&quot;,<br>&quot;options&quot;: &quot;customer_type&quot;<br>}</pre><p><strong>Fix:</strong> Use Link for truly fixed relationships. Dynamic Link adds complexity.</p><h3><strong>Performance Considerations</strong></h3><p><strong>Question:</strong> Does Dynamic Link have performance overhead?</p><p><strong>Answer:</strong> Negligible. The DocType lookup is a simple field read. The real performance considerations are:</p><p>1. <strong>Index the Dynamic Link field</strong> for large tables</p><p>2. <strong>Batch your queries</strong> to avoid N+1 problems (which I did in my implementation)</p><p>3. <strong>Use proper joins</strong> when querying related documents</p><h3><strong>Real-World Benefits I Experienced</strong></h3><p>After implementing Dynamic Link in the Stock Ledger report:</p><p>✅ <strong>One column</strong> instead of two separate “Sales Invoice” and “Purchase Invoice” columns</p><p>✅ <strong>Clickable links</strong> that navigate to the correct document type</p><p>✅ <strong>Future-proof</strong> — easy to add new invoice types if needed</p><p>✅ <strong>Cleaner code</strong> — no complex if/else logic for different link types</p><p>✅ <strong>Better UX</strong> — users see a unified “Invoice” column regardless of transaction type</p><h3><strong>Key Takeaways</strong></h3><p>1. <strong>Link</strong> = Fixed relationship to one DocType (use for 80% of cases)</p><p>2. <strong>Dynamic Link</strong> = Variable relationship determined at runtime (use when you need flexibility)</p><p>3. <strong>Always create the companion field</strong> that Dynamic Link references</p><p>4. <strong>Think future-proof</strong> — if requirements might change, start with Dynamic Link</p><p>5. <strong>Don’t over-engineer</strong>— use simple Link fields when the relationship is truly fixed</p><h3><strong>Conclusion</strong></h3><p>Understanding Link vs Dynamic Link is crucial for building flexible, maintainable ERPs with Frappe. While Link fields are your bread-and-butter for fixed relationships, Dynamic Link fields give you the polymorphic power to handle variable relationships elegantly.</p><p>The next time you’re designing a field that links to another document, ask yourself: “Could this ever point to a different DocType?” If the answer is yes, or even maybe, reach for Dynamic Link.</p><p>Your future self (and your users) will thank you.</p><p><strong>About the Implementation: </strong>This article is based on a real enhancement I made to ERPNext’s Stock Ledger report, adding Invoice and Invoice Date columns that intelligently link to Sales Invoices for Delivery Notes and Purchase Invoices for Purchase Receipts — all using a single Dynamic Link field.</p><p><em>Have you encountered interesting use cases for Dynamic Link in your Frappe projects? Share your experiences in the comments!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=baca3f0b8afd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>