0

In LWCs, we can import an object reference like:

import MY_CUSTOM_OBJECT from '@salesforce/schema/My_Custom_Object__c';

Can we do the same for platform events?

e.g.

import MY_PLATFORM_EVENT_OBJECT from '@salesforce/schema/My_Platform_Event__e';

The reason I ask is because I want to use it in my subscribe call. (A bit of an X-Y problem surrounding multiple 1GP packages, namespaces, etc.)

Use Case

I have LWCs that use platform events that I'd like to use across multiple namespaced 1GP packages, and potentially subscriber org (without a namespace).

This is a bit of a follow up question to my previous question: Does a Managed Package LWC that subscribes to platform event need the namespace?

4
  • A question, why cant this be done by a simple custom label? like taking the objectAPIName directly from Custom label? And import custom label and subscribe to it? '/event/'+IMPORTED_CUSTOM_LABEL Sep 6, 2023 at 2:38
  • @NagendraSingh That falls into the namespace issue, presumably.
    – sfdcfox
    Sep 6, 2023 at 2:40
  • @NagendraSingh Because my problem is surrounding the namespace, which would therefore need to be added to the custom label
    – Nick C
    Sep 6, 2023 at 2:42
  • @NickCook Makes sense. I as thinking as the custom label can be imported without a namespace {!$Label.c.<label_name>}, thus giving you the flexibility to change this label as a post step. Definitely more complex than what you have already done. Sep 6, 2023 at 2:57

1 Answer 1

0

Short answer - No. There doesn't appear to be a way to import the platform event object.

There is some weird behaviour that I've included here for reference.

Attempts to import the platform event object

You can import it and it will deploy successfully.

import MY_PLATFORM_EVENT_OBJECT from '@salesforce/schema/My_Platform_Event__e';

However, when you retrieve the LWC containing this code, the __e is replaced with __c. If you then try to deploy that, the deployment will fail.

My first idea was to ignore this change, but when I released this as part of my managed package, the installation in subscriber org failed because it's trying to import NAMESPACE__My_Platform_Event__c, which doesn't exist.

How did I get it to work?

In the end, I needed this code to work across multiple 1GP manage packages, so I had to make the namespace part dynamic.

I have a centralised Apex method that gets the namespace for me, which is what I used in my logic.

This approach works in the managed package development org, and in multiple subscriber orgs.

import getOurNamespace from '@salesforce/apex/Utils.getOurNamespace';

export default class MyLwc extends LightningElement {

    subscription;

    ourNamespace;
    @wire( getOurNamespace )
    wiredGetOurNamespace( result) {
        this.ourNamespace = result;

        this.subscribeToPlatformEvent();
    }
    
    subscribeToPlatformEvent() {
    
        if(this.ourNamespace.data && !this.subscription) {
    
            const platformEventObjectApiName = [this.ourNamespace.data, 'RecordChange__e'].join('__');
            
            subscribe(`/event/${platformEventObjectApiName }`, -1, (response) => { /* do things */ })
            then((response) => {
                this.subscription = response;
            })
        }
    }
    
}

You must log in to answer this question.

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