{"id":12106,"date":"2026-01-13T05:52:14","date_gmt":"2026-01-13T05:52:14","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=12106"},"modified":"2026-01-02T05:52:52","modified_gmt":"2026-01-02T05:52:52","slug":"understanding-namespace-behavior-in-salesforce-second-generation-packaging-2gp","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2026\/01\/13\/understanding-namespace-behavior-in-salesforce-second-generation-packaging-2gp\/","title":{"rendered":"Understanding Namespace Behavior in Salesforce Second Generation Packaging (2GP)"},"content":{"rendered":"\n<p>If you\u2019re transitioning to Salesforce Second-Generation Packaging (2GP), one of the first things you\u2019ll notice is that your metadata looks different after packaging. Objects, fields, Apex classes, and components suddenly appear with a namespace prefix, even though your original source code contains no such prefix.<\/p>\n\n\n\n<p>This can be confusing at first but this behavior is completely expected. In this blog, we\u2019ll explain why Salesforce automatically injects namespaces in 2GP, how it rewrites code internally, and why everything works seamlessly even though you never see the transformed code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">What This Blog Covers<\/mark><\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Why 2GP automatically adds a namespace<\/li>\n\n\n\n<li>How Salesforce rewrites code internally<\/li>\n\n\n\n<li>Why you don\u2019t see namespaced code after installation<\/li>\n\n\n\n<li>Why the code works without namespace prefixes<\/li>\n\n\n\n<li>Why the namespace never appears in your Git repository<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Why 2GP Automatically Adds the Namespace<\/mark><\/strong><\/h2>\n\n\n\n<p>When you create a 2GP managed package, Salesforce automatically applies the package namespace to all metadata included in the package, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Apex classes<\/li>\n\n\n\n<li>Lightning Web Components (LWC)<\/li>\n\n\n\n<li>Custom objects<\/li>\n\n\n\n<li>Custom fields<\/li>\n\n\n\n<li>Permission sets<\/li>\n\n\n\n<li>Any additional packaged metadata<\/li>\n<\/ul>\n\n\n\n<p>With 2GP, the namespace is mandatory and is applied at build time. You never add the namespace manually in your code &#8211; Salesforce handles it automatically.<\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2025\/12\/blogImg1.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2025\/12\/blogImg1.png\" alt=\"Namespace Handling in Package\"><\/a><\/center><font size=\"2\"><center><i>Namespace Handling in Package<\/i><\/center><\/font>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">How Salesforce Rewrites Code Internally<\/mark><\/strong><\/h2>\n\n\n\n<p>Even though your source code does not contain a namespace, Salesforce injects it during the packaging process.<\/p>\n\n\n\n<p>For example, if your Apex code looks like this:<\/p>\n\n\n\n<p><strong>insert new Invoice__c(Amount__c = 100);<\/strong><\/p>\n\n\n\n<p>During packaging, Salesforce internally transforms it into:<\/p>\n\n\n\n<p><strong>insert new abc__Invoice__c(abc__Amount__c = 100);<\/strong><\/p>\n\n\n\n<p>Here, <strong>abc<\/strong> represents the package namespace.<\/p>\n\n\n\n<p>You should never make this change yourself. This transformation happens only within the compiled package &#8211; not in your source repository.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Why You Don\u2019t See the Namespaced Code After Installation<\/mark><\/strong><\/h2>\n\n\n\n<p>Second-Generation Packaging is source-driven, which means:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Subscriber orgs receive compiled components, not your original source<\/li>\n\n\n\n<li>The namespaced version of the code is not visible<\/li>\n\n\n\n<li>Installed metadata is fully namespaced, even though you didn\u2019t write it that way<\/li>\n<\/ul>\n\n\n\n<p>This often leads developers to wonder how everything still works when no namespace was added manually.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Why the Code Still Works Without Namespace Prefixes<\/mark><\/strong><\/h2>\n\n\n\n<p>Salesforce automatically updates all internal references during packaging, including:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Object references<\/li>\n\n\n\n<li>Field references<\/li>\n\n\n\n<li>Apex class references<\/li>\n\n\n\n<li>LWC references<\/li>\n<\/ul>\n\n\n\n<p>As long as the references are within the same package, Salesforce ensures they are correctly namespaced.<\/p>\n\n\n\n<p>That\u2019s why your code behaves exactly as expected without any manual changes. Behind the scenes, Salesforce adjusts everything automatically, even though the installed version appears fully namespaced.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Why the Namespace Doesn\u2019t Appear in Your Git Repository<\/mark><\/strong><\/h2>\n\n\n\n<p>You might expect your Git repository to contain namespaced code\u2014but it never will. Your repo always remains namespace-agnostic.<\/p>\n\n\n\n<p>This is because namespaces are applied only at packaging time. Keeping the source clean ensures that it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Works in scratch orgs<\/li>\n\n\n\n<li>Works without a namespace<\/li>\n\n\n\n<li>Works with future packages<\/li>\n\n\n\n<li>Avoids hard-coded prefixes<\/li>\n<\/ul>\n\n\n\n<p>This design is intentional and follows Salesforce best practices.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Conclusion\u00a0<\/mark><\/strong><\/h2>\n\n\n\n<p>Salesforce Second-Generation Packaging may seem confusing at first, especially when you see namespaced metadata in subscriber orgs while your source code contains no namespace references. However, this behavior is predictable, consistent, and fully automated.<\/p>\n\n\n\n<p>With 2GP, Salesforce always applies the package namespace at build time. Developers should never manually add namespace prefixes in their source code. Salesforce injects namespaces internally, ensuring that installed packages are properly namespaced, protected, and isolated &#8211; while keeping your repository clean, reusable, and future-proof.<\/p>\n\n\n\n<p>By following the above blog instructions, you will be able to learn <strong>\u201cUnderstanding Namespace Behavior in Salesforce Second Generation Packaging (2GP)\u201c<\/strong>. If you still have queries or any related p roblems,don\u2019t hesitate to contact us at <a href=\"mailto:salesforce@greytrix.com\" target=\"_blank\" rel=\"noreferrer noopener\">salesforce@greytrix.com<\/a>. More details about our integration product are available on our website and <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noreferrer noopener\">Salesforce AppExchange<\/a>.<\/p>\n\n\n\n<p>We hope you may find this blog resourceful and helpful. However, if you still have concerns and need more help, please contact us at <a href=\"mailto:salesforce@greytrix.com\" target=\"_blank\" rel=\"noreferrer noopener\">salesforce@greytrix.com<\/a>.<\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, Custom App development and Technical Support business partners and end users. Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019re transitioning to Salesforce Second-Generation Packaging (2GP), one of the first things you\u2019ll notice is that your metadata looks different after packaging. Objects, fields, Apex classes, and components suddenly appear with a namespace prefix, even though your original source code contains no such prefix. This can be confusing at first but this behavior is\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2026\/01\/13\/understanding-namespace-behavior-in-salesforce-second-generation-packaging-2gp\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[2702,2697,2701,2481,2699,2703,2698,2608,2705,2700,2696,2704],"class_list":["post-12106","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-lwc-namespace-salesforce","tag-managed-packages-salesforce","tag-namespace-behavior-salesforce","tag-salesforce-2gp","tag-salesforce-apex-namespace","tag-salesforce-isv-development","tag-salesforce-metadata-packaging","tag-salesforce-namespace","tag-salesforce-packaging-architecture","tag-salesforce-packaging-best-practices","tag-salesforce-second-generation-packaging","tag-salesforce-source-driven-development"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/12106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=12106"}],"version-history":[{"count":11,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/12106\/revisions"}],"predecessor-version":[{"id":12118,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/12106\/revisions\/12118"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=12106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=12106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=12106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}