2

I have a question in managed package.

Let say I am writing a SOQL query in Apex class as

/* Apex */
[SELECT Id, custom1__c, custom2__c, custom3__c FROM CustomObject__c];

/* VF or Aura */
<input value="{obj.custom1__c}" />

/*Script */
var a = obj.custom1__c;

Here let's say I am creating a Managed package along with this Class and has NameSpace as abc__ Once installed in an org how does the above field or object behaves? Will I get the correct data without Error.

What are the components (Like Aura components, VF page, Apex) that get affected when installed and If NameSpace was not considered while developing the code?

What is the consideration for this?

Dynamic query with the namespace method is the correct option?

1 Answer 1

6

Generally speaking the managed package code base does not need to explicitly provide namespace prefixing (for Apex classes, custom objects, custom fields, field sets etc.). The Salesforce runtime understands the context of the class/object/field reference and will automatically apply the namespace prefix transparently when done within the package's code base.

LWCs have to be written using the "c-" namespace prefix "placeholder". LWCs in a managed package can be set "exposed" and thus can be dropped into Lightning Pages using the Lightning App Builder, but you cannot use a custom LWC from a package in the template of an LWC not in that package. In terms of accessing SObject data directly within LWC components using uiRecordApi's getRecord (for example) you would need to use namespaced access. You could do it like:

const field = data.fields.namespace__FieldName__c || data.fields.FieldName__c;
const value = field.value;

This allows the LWC to work with or without namespacing. However, that's not important if you plan to permanently namespace the code.

Aura is a slightly different issue; if you want to reference your components (e.g. using LightningOut and Visualforce, or when referencing another component from a component's cmp markup) you have to write reference your Aura components using the package's namespace prefix. Thus, if you wrote your code with two Aura components, "parent" and "child", and had the parent markup like:

<aura:component ...>
    <c:child ...></c:child>
</aura:component>

This will fail when you package it. You must change it to use the namespace prefix (e.g. "prefix") thus:

<aura:component ...>
    <prefix:child ...></prefix:child>
</aura:component>

Thus, your SOQL, VF and Apex will work as-is, but you have to namespace your Aura components and consider how SObject data access works in LWC. Any use of the (global) classes, objects or fields etc. outside the package will, however, need to include the namespace prefix in order to work.

UPDATE: With the recent (2022) introduction of Lightning Web Security as a replacement for the Locker Service, cross-namespace LWC usage becomes possible. See this overview comparison documentation for details. This means you should be able to use a component from a package in the template markup for a component outside that package. There are documented limitations.

6
  • Thanks for the detailed explanation and for your time. I have a doubt about that. Let's say I referred a field in JS like obj.field__c. In this case, does it make any problem?
    – SFDC
    Commented Jul 20, 2020 at 8:49
  • What sort of JavaScript are you talking about? Some static resource JavaScript, or do you mean in Aura/LWC (and if so which)?
    – Phil W
    Commented Jul 20, 2020 at 10:03
  • I added some detail on SObject field data access in LWCs when using standard things like uiRecordApi.
    – Phil W
    Commented Jul 20, 2020 at 10:17
  • 1
    No, I mean let's say I am querying the account and bunch of objects in JS either in Aura and LWC and I am accessing the fields as var fldValue = acc.field1__c;.In that case, will it be a problem? And what if I use as <span>{acc.field__c}<span/> . Will I get the correct data. even though I didn't namespace?
    – SFDC
    Commented Jul 20, 2020 at 11:48
  • 1
    Just before I tried a sample package with one Component in which I tried to display field data using <span> {acc.Field__c} </span>. After installing as a beta package with nameSpace. The data was not displayed.
    – SFDC
    Commented Jul 20, 2020 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .