4

I'm trying to make a component in a managed package to subscribe to a certain platform event, and referencing the channel with its API name previously worked:

import BATCH_EVENT_NAME from '@salesforce/schema/BatchExportEvent__e'

subscribe('/event/BatchExportEvent__e', -1, eventCallback).then(response => {
       component.subscribed = true
       component.subscription = response
})

But trying to use ${BATCH_EVENT_NAME.objectApiName} to subscribe "dynamically" to the event, doesn't work. The framework returns a BatchExportEvent__c string (note the c suffix, instead of the e, for events).

Hardcoding the BatchExportEvent__e to the subscription seems to solve the issue in a development environment. However, I need to access the event's fields as well. So of course I tried to add those fields using the import syntax as well. For example:

import STATUS_FIELD from '@salesforce/schema/BatchExportEvent__e.Status__c'

And, again, development went smoothly. But when trying to install a package version in a development environment, I get the error:

exportData: Invalid reference BatchExportEvent__c.JobItemsProcessed__c
of type sobjectClass in file exportData.js

Notice, once more, the issue with the suffix. The platform appends c instead of e. I'm not even sure if this is the issue here anymore.

Is there a guide on how to use platform events within managed packages, and accessing them in the component?


Like mentioned in the comments, if I try to add the namespace manually I get the following message while pushing the code to the scratch org:

force-app/main/frontend/lwc/exportData/exportData.js  LWC1504:
Invalid module id "x__BatchExportEvent__e.x__AsyncApexJobId__c"
for type "schema". Explicit use of namespace "x" in file 
"exportData.js" is prohibited. Use default namespace "c" instead.
6
  • Would adding your namespace to the object and field name be any helpful? import STATUS_FIELD from '@salesforce/schema/namespace__BatchExportEvent__e.namespace__Status__c
    – Raul
    Commented May 11, 2020 at 23:00
  • If I remember correctly the platform doesn’t allow me to save it if I specify the namespace. I only got the error for apex classes though, so I need to check this with objects and platform events. Commented May 11, 2020 at 23:02
  • 1
    I've updated the question with what happens when I attempt to add the namespace explicitly, @Raul. Commented May 12, 2020 at 11:32
  • Oh damn, and does it allow us to use namespace c__?
    – Raul
    Commented May 12, 2020 at 12:15
  • 1
    I would love to hear from someone at SF or otherwise knowledgeable on this topic. It seems to be a pretty basic problem. I want to write/test my code so it works both in a dev (non-namespaced) org and a packaged (namespaced) org. I can't hardwire the channel name because it is different depending on whether or not I am in a namespaced org or not. How do I even determine, from an LWC, whether I am in a namespaced org or not? Our hacky solution is to listen to both events, or to have the server send us the event name...
    – kamezaburo
    Commented Sep 23, 2020 at 17:00

1 Answer 1

2

Just heard from SF... The point is that the only suffix supported in LWC is '__c' so far. It is not yet documented in Importing Limitations section here: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.data_wire_service_about. Importing Platform Events (with '__e' suffix) should soon be added as a part of Importing Limitations sections into this doc. Hopefully, it will be supported in one of future releases.

So, it is not a limitation applied to a managed package. It is a general LWC limitation. If you try to deploy a LWC with '__e' suffix with Metadata API to any org, the suffix would be replaced with '__c' it the target org because of this limitation.

2
  • That's good to know. This is not a known issue then, but comes from an internal response from a case, right? Would you be willing to update this question with more information regarding what they responded to you? Commented Oct 28, 2020 at 16:26
  • @RenatoOliveira that is correct, it comes from a case. I opened it to force them create a known issue, but it appeared to be a Doc bug according to their answer.
    – kumeskyi
    Commented Oct 29, 2020 at 7:50

You must log in to answer this question.

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